2014-09-27 53 views
0

我想要做的是當用戶點擊php選擇option.then 它的值應該傳遞到另一個頁面作爲變量。通過PHP選擇選項框值到另一頁

有一個代碼我是 嘗試。

<table> 
<tr><td> 
<?php 
mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("member")or die(mysql_error()); 
$result = mysql_query("SELECT id,ugroup FROM ugroups"); 

?> 

<select name='group' id='group' > 
<?php 
while ($line = mysql_fetch_array($result)) { 
echo '<option value=' . $line['ugroup'] . '>' . $line['ugroup'] . '</option>'; 

}?> 
</select> 
<a href="db_sql/add_group.php?val=<?php echo $line['ugroup'];?>">click </a> 
</td></tr> 
</table> 

這是add_group.php代碼

<?php 
    session_start(); 
    ob_start(); 
    include('../limoconfig.php'); 
    if(!isset($_SESSION['adminuserid'])) 
    { 
     header('Location:../index.php'); 
    } 
    else 
    { 
    if($_GET) 
    { 
     $ugroup = $_GET['ugroup']; 
     /*$id = $_GET['id'];*/ 

     $acc_status = "update users set ugroup='".$ugroup."' where id=1931"; 
     echo $acc_status; 
     $rate = db::getInstance()->exec($acc_status); 
     header('Location:../home.php'); 
    } 

    } 
    ?> 

它不工作。

+1

嘗試用AJAX .. – 2014-09-27 06:48:10

+0

你的目標是留在同一頁上?還是要去下一頁?你的目標還不清楚。 – Dorvalla 2014-09-27 07:25:12

+0

我想用變量去下一頁。 – Tje 2014-09-27 10:37:49

回答

0

您可以使用ajax或只使用html表單。

如AJAX的:只是一個簡單的比如,你可以根據你的需要

<html> 
<head> 

function myAjax(str) 
{ 

if (str=="") 
    { 
    document.getElementById("results").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("results").innerHTML=xmlhttp.responseText; 

    } 
    } 
xmlhttp.open("GET","add_group.php?ugroup="+str,true); // your next page 

xmlhttp.send(); 
} 

function myFunction() { 
    var x = document.getElementById("group").value; 
    myAjax(x); 
} 

    </script> 

</head> 
<body> 


<table> 
<tr><td> 
<?php 
mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("member")or die(mysql_error()); 
$result = mysql_query("SELECT id,ugroup FROM ugroups"); 

?> 

<select name='group' id='group' > 
<?php 
while ($line = mysql_fetch_array($result)) { 
echo '<option value=' . $line['ugroup'] . '>' . $line['ugroup'] . '</option>'; 

}?> 
</select> 
<a href="#" onclick="myFunction()">click </a> 



</td></tr> 
</table> 

<div id="results"> <!-- the echo of add_group.php gets displayed here --> </div> 

</body> 
</html> 
+0

它不工作prashant – Tje 2014-09-27 11:25:54

+0

你可以提供** add_group.php **代碼 – 2014-09-27 11:50:45

+0

我已添加到我的問題 – Tje 2014-09-27 11:59:42

3

首先你需要把內 你的表的修改,然後你可以調用阿賈克斯的錨的點擊( )。

jQuery(document).ready(function($) { 
var data=''; 
$("#group option:selected").each(function() { 
    if ($(this).attr('value') !== '') { 
     data=$(this).attr('value'); 
    } 
}); 
$("a").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "file.php", 
     data: data, 
     beforeSend: function() { 

      $('body').css("opacity", "0.3"); 
     }, 
     success: function(response) { 
      alert(response); 
     }, 
     complete: function() { 
      $('body').css("opacity", "1"); 
     } 
    }); 
}); 

});

+0

他需要在你的代碼中包含Jquery;)你可能想在你的答案中加入這個);我也會爲你的點擊事件定義一個ID,因爲它會完全在其他錨鏈接上發狂。 – Dorvalla 2014-09-27 07:23:37

+0

你可以包括這個爲我的code.please.i'm新到網絡開發 – Tje 2014-09-27 14:04:01

相關問題