gpt4 book ai didi

javascript - PHP数据在AJAX中成功吗?

转载 作者:行者123 更新时间:2023-11-28 15:30:14 26 4
gpt4 key购买 nike

我正在尝试通过 AJAX 运行非常基本的 PHP 代码,并将数据从 PHP 页面返回到 AJAX 成功中。

但是,我没有从 PHP 页面获得 AJAX 成功,这让我很烦恼。

这是 AJAX 代码:

$(document).ready(function(){
$(function(){
$('#form-post').on('submit', function(e){

// prevent native form submission here
e.preventDefault();

// now do whatever you want here
$.ajax({
type: $(this).attr('method'), // <-- get method of form
url: $(this).attr('action'), // <-- get action of form
data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
//$('#result').html('<img src="loading.gif" />');
},
success: function(data){
$('#messageme').html(data);
}
});
});
});
});

这是表格:

    <form id="form-post"  action="post-code.php" method="post" >
<input type="hidden" value="Post" name="submit" />
<input type="text" class="inp-form" name="postcode" id="postcode" placeholder="Enter Post Code " /><br /><br /><input type="text" id="messageme" /><br /><br />
<input id="findAd" type="button" value=" Search For Address" />
</form>

和一个非常简单的 php:

<?php

$street = "some";
echo $street;
?>

有人可以就此提出建议吗?

最佳答案

请尝试以下操作:只需将 type="button" 更改为 type="submit"

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

<body>
<form id="form-post" action="post-code.php" method="post" >
<input type="hidden" value="Post" name="submit" />
<input type="text" class="inp-form" name="postcode" id="postcode" placeholder="Enter Post Code " /><br /><br />
<input type="text" id="messageme" /><br /><br />
<input id="findAd" type="submit" value=" Search For Address" />
</form>
<script>$(document).ready(function(){

$(function(){
$('#form-post').on('submit', function(e){

// prevent native form submission here
e.preventDefault();

// now do whatever you want here
$.ajax({
type: $(this).attr('method'), // <-- get method of form
url: $(this).attr('action'), // <-- get action of form
data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
//$('#result').html('<img src="loading.gif" />');
},
success: function(data){
alert(data);
$('#messageme').html(data);
}
});
});
});
});
</script>


</body>
</html>

关于javascript - PHP数据在AJAX中成功吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27657277/

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