[英]How to get a value from select with Ajax and use in PHP
As I wrote in the title I have this part of code in page page.php that is in a subfolder admin.正如我在标题中所写的那样,我在子文件夹 admin 中的 page.php 页面中有这部分代码。 So the path of page is ../admin/page.php:所以页面的路径是../admin/page.php:
<select name="my_select" id="my_select" onchange="function(this.value);">
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
}
?>
</select>
$var = $_POST['my_select'];
echo "I have selected $var";
I use a function that I have found on internet:我使用我在互联网上找到的一个函数:
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'page.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("my_select").innerHTML=response;
}
});
}
What I have to do to take value in $var?我必须做什么才能获得 $var 的价值? Because I need this value to build other things.因为我需要这个值来构建其他东西。 Is it possible?是否可以?
EDIT: Probably I don't explain very well my problem.编辑:可能我没有很好地解释我的问题。 I don't have very good with ajax because I never use it.我对 ajax 不是很好,因为我从不使用它。 I have a deadline so I can't study it now.我有一个截止日期,所以我现在不能学习。 Now I have this situation: I have a select-form with an input submit.现在我有这种情况:我有一个带有输入提交的选择表单。 After click on the button I use $_POST['myoption'] and I get the value.单击按钮后,我使用 $_POST['myoption'] 并获得值。 Then I do it:然后我这样做:
if($var == 1)
//a query from database
else if($var == 2)
//another different query
else
//other but no query
This work correctely.这项工作正确。 I need to change it and use in the same page.我需要更改它并在同一页面中使用。 How can I do the same?我怎样才能做同样的事情?
You don't to do a POST to do this you can do it with jQuery.你不需要做一个 POST 来做到这一点,你可以用 jQuery 来做到这一点。
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
?>
<select name="my_select" id="my_select">
<?php
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
?>
</select>
<span id="selected"></span>
<script>
$("#my_select").change(function() {
$("#selected").html($("#my_select option:selected").text());
});
</script>
This will give the select value to PHP:这将为 PHP 提供选择值:
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
if (isset($_POST['my_select'])) {
$var = $_POST['my_select'];
} else {
$var = '';
}
?>
<form id="my_form" action="" method="POST">
<select name="my_select" id="my_select">
<?php
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
?>
</select>
</form>
<span id="selected">I have selected <?php echo $var; ?></span>
<script>
$("#my_select").change(function() {
$('#my_form').submit();
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.