繁体   English   中英

将PHP选择选项框的值传递到另一页

[英]Pass PHP select option box values to another page

我想做的是当用户单击php select option.then的值应作为变量传递到另一页。

我正在尝试输入代码。

<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'); 
    } 

    }
    ?>

它不起作用。

首先,您需要将表格放入其中,然后单击锚点()即可调用ajax。

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");
        }
    });
});

});

您可以使用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>

将其包装在一个表单标签中,然后创建一个提交按钮而不是链接,它可以通过POST或GET发送。

该元素是一个表单控件,可以在表单中使用以收集用户输入。

http://www.w3schools.com/tags/tag_select.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM