gpt4 book ai didi

javascript - 在Ajax中如何将html页面中的国家名称传递到php页面?

转载 作者:行者123 更新时间:2023-11-28 08:42:57 26 4
gpt4 key购买 nike

How to pass country name in html page to php page using ajax.

演示.php

<html>
<head>
<title>Dynamic Form</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<script>
$(document).ready(function(){
$("form").on('submit',function(event){
event.preventDefault();

data = $(this).serialize();
$.ajax({
type: "GET",
url: "post.php",
data: data
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
</script>

</head>
<body>

<form>
<table>
<tr>
<td>
<select name="one" onchange="if (this.value=='other'){this.form['other'].style.visibility='visible';this.form['submit'].style.visibility='visible'}else {this.form['other'].style.visibility='hidden';this.form['submit'].style.visibility='hidden'};"



<option value="" selected="selected">Select...</option>
<option value="India">India</option>
<option value="Pakistan">Pakistan</option>
<option value="Us">Us</option>
<option value="other">Other</option>
</select>
<input type="textbox" name="other" id="other" style="visibility:hidden;"/>
<input type="submit" name="submit" value="Add Country" style="visibility:hidden;"/>

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

</body>

post.php

<?php

if(isset($_POST['submit']))
{
$Country = $_POST['other'];
echo $Country;
}
?>

When user select other from drop down list at that time display one textbox and one submit button.when user click on submit button at that time pass country name of demo.php to post.php using ajax.

最佳答案

您有一些错误。 $_POST['submit'] 永远不会与 data = $(this).serialize(); 一起发布。所以你需要检查其他东西。另外,您在 AJAX 中将 GET 定义为方法,但在 PHP 中检查 $_POST。所以改变两者之一。像这样的东西:

HTML/JavaScript:

<html>
<head>
<title>Dynamic Form</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<script>
$(document).ready(function(){
$("form").on('submit',function(event){
event.preventDefault();
data = $(this).serialize();
$.ajax({
type: "POST",
url: "post.php",
data: data
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
</script>

</head>
<body>

<form>
<table>
<tr>
<td>
<select name="one" onchange="if (this.value=='other'){this.form['other'].style.visibility='visible';this.form['submit'].style.visibility='visible'}else {this.form['other'].style.visibility='hidden';this.form['submit'].style.visibility='hidden'};">
<option value="" selected="selected">Select...</option>
<option value="India">India</option>
<option value="Pakistan">Pakistan</option>
<option value="Us">Us</option>
<option value="other">Other</option>
</select>
<input type="textbox" name="other" id="other" style="visibility:hidden;"/>
<input type="submit" name="submit" value="Add Country" style="visibility:hidden;"/>
</td>
</tr>
</table>
</form>

</body>

PHP:

<?php
if(isset($_POST['other'])) {
$Country = $_POST['other'];
echo $Country;
}
?>

关于javascript - 在Ajax中如何将html页面中的国家名称传递到php页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com