![](/img/trans.png)
[英]how to send a value from php select option to another php page using ajax?
[英]How to send the value of select tag using ajax to php residing on the same page
問題
我想使用select
標記來選擇一個值,並使用ajax調用將該值發送到嵌入在同一頁面中的PHP。
在這里,在我的代碼中,我正在使用select標記,並且一旦從下拉列表中選擇了任何值,該值就會被發送到javascript並得到適當的警報。 但是當我在PHP中使用$ _POST ['option']並回顯它時,它並沒有打印在頁面上。
我知道PHP是一種服務器端語言,並且在頁面加載時,任何用PHP編寫的內容都會首先執行。
tablesize.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0 /jquery.min.js"></script>
<script type="text/javascript">
function fetch_select(val){
$.ajax({
type: 'post',
url: 'tablesize.php',
datatype:'json',
data: {option:val},
success: function (response) {
alert(val);
//document.getElementById("new_select").innerHTML=response;
}
});
}
</script>
</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center>
<div id="select_box">
<select onchange="fetch_select(this.value);">
<option value="10">state</option>
<option value="20">20</option>
</select>
</div>
</body>
</html>
<?php
if(isset($_POST['option'])){
$val=$_POST['option'];
echo $val;
}
?>
謝謝...
同一頁面的ajax調用和回顯值
<?php
session_start();
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document).on("change","#sel",function(){
var val = $(this).val();
console.log(val);
$.ajax({
type: 'post',
url: 'index.php?action=data',
datatype:'json',
data: {"option":val},
success: function (response) {
alert(val);
location.href="index.php";
}
});
});
});
</script>
</head>
<body>
<div id="select_box">
<select id="sel" name="sel">
<option <?php if($_SESSION['val']=="a") echo "selected" ;?> value="a">a</option>
<option <?php if($_SESSION['val']=="b") echo "selected" ;?> value="b">b</option>
<option <?php if($_SESSION['val']=="c") echo "selected" ;?> value="c">c</option>
<option <?php if($_SESSION['val']=="d") echo "selected" ;?> value="d">d</option>
<option <?php if($_SESSION['val']=="e") echo "selected" ;?> value="e">e</option>
<option <?php if($_SESSION['val']=="e") echo "selected" ;?> value="f">f</option>
</select>
</div>
</body>
</html>
<?php
$val = $_POST['option'];
if($_REQUEST['action']=="data"){
$_SESSION['val']=$val;
echo "Selected value = ".$_SESSION['val'];
}elseif ($val=="") {
echo "Selected value = ".$_SESSION['val'];
}
?>
不同的ajax.php,在此處編寫代碼,並在ajax代碼中更改url。
Ajax.php
<?php
if(isset($_POST['option']))
{
$val=$_POST['option'];
echo $val;
}
?>
你的ajax代碼...
function fetch_select(val){
$.ajax({
type: 'post',
url: 'ajax.php',
datatype:'json',
data: {option:val},
success: function (response) {
alert(response);
//document.getElementById("new_select").innerHTML=response;
}
});
}
這是您的解決方案
Ajax成功包含您的結果。 錯誤出在您的打印方法中。
您需要為ajax使用其他文件,這里我使用ajax.php 。
還有一件事情總是將腳本放在HTML頁面上。
tablesize.php
<html>
<head>
</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center></center>
<div id="select_box">
<select onchange="fetch_select(this.value);">
<option value="10">state</option>
<option value="20">20</option>
</select>
</div>
<p id="print-ajax"></p><!--Result will print here-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0 /jquery.min.js"></script>
<script type="text/javascript">
function fetch_select(val){
$.ajax({
type: 'post',
url: 'ajax.php',
datatype:'json',
data: {option:val},
success: function (response) {
$('#print-ajax').html(response);//This will print you result
}
});
}
</script>
</body>
</html>
ajax.php
echo $_POST['option'];die;
您在ajax發布成功后打印了錯誤的變量。 您需要response
變量來警告PHP腳本的輸出。
還要將PHP post handler塊放在頁面頂部,以便HTML內容不會包含在AJAX的響應中。
更新的代碼:
<?php
if(isset($_POST['option'])){
$val=$_POST['option'];
echo $val; exit;
}
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function fetch_select (val){
$.ajax({
type: 'post',
url: 'tablesize.php',
datatype:'json',
data: { option:val },
success: function (response) {
alert(response);
}
});
}
</script>
</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center>
<div id="select_box">
<select onchange="fetch_select(this.value);">
<option value="10">state</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.