gpt4 book ai didi

javascript - 如何在同一页面调用ajax页面?

转载 作者:可可西里 更新时间:2023-11-01 01:02:25 26 4
gpt4 key购买 nike

我有两个文件 demo.phppost.php。单例怎么办 页而不是两页。

demo.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: "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>

post.php

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

如何在不将数据从一个页面传递到另一个页面的情况下使用 demo.php 中的 post.php 数据。

最佳答案

改变你的ajax url

$.ajax({
type: "POST",
url: "demo.php",
data: data
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});

并将其添加到您的 demo.php

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

关于javascript - 如何在同一页面调用ajax页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20351075/

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