jQuery提交PHP未执行 [英] JQuery to submit PHP not executing
问题描述
经过数小时的研究,让我惊讶的是我的JQuery根本没有执行.
After hours of playing with this, it hit me that my JQuery simply isn't executing.
我有一个页面正在尝试提交给PHP脚本,而没有刷新/离开页面.如果我使用典型的表单操作/方法/提交,它将很好地插入到我的数据库中.但是当我使用JQuery时,JQuery根本不会运行.警报不显示. (我是JQuery的新手).我试图对此进行研究,但没有任何效果.
I have a page that I am trying to submit to a PHP script without refreshing/leaving the page. If I use a typical form action/method/submit, it inserts into my database just fine. But when I use JQuery, the JQuery will not run at all. The alert does not show. (I'm new to JQuery). I have tried to research this, but nothing is working.
这是我的主页:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$('submitpicks').on('submit','#submitpicks',function(e){
e.preventDefault(); //this will prevent reloading page
alert('Form submitted Without Reloading');
});
});
</script>
</head>
<body>
<form name="submitpicks" id="submitpicks" action="" method="post">
<script language="javascript">
var v=0;
function acceptpick(thepick,removepick){
var userPick = confirm("You picked " + thepick + ". Accept this pick?");
//var theid = "finalpick" + v;
var removebtn = "btn" + removepick;
//alert(theid);
if(userPick==1){
document.getElementById("finalpick").value=removepick;
document.getElementById(removebtn).disabled = true;
document.getElementById("submitpicks").submit();
v=v+1;
}
}
</script>
<?php
include "Connections/myconn.php";
//$setid = $_SESSION["gbsid"];
$setid = 11;
$setqry = "Select * from grabBagParticipants where gbsid = $setid order by rand()";
$setresult = mysqli_query($conn, $setqry);
$u=0;
if(mysqli_num_rows($setresult)>0){
while($setrow = mysqli_fetch_array($setresult)){
//shuffle($setrow);
echo '<input type="button" name="' . $setrow["gbpid"] . '" id="btn' . $setrow["gbpid"] . '" value="' . $u . '" onClick=\'acceptpick("' . $setrow["gbpname"] . '", ' . $setrow["gbpid"] . ');\' /><br />';
$u=$u+1;
}
}
?>
<input type="text" name="finalpick" id="finalpick" />
<input type="submit" value="Save" />
</form>
<div id="results"> </div>
</body>
</html>
这是我的PHP:
<?php
include "Connections/myconn.php";
$theGiver = 1;
$theReceiver = $_POST['finalpick'];
$insertsql = "insert into grabBagFinalList(gbflgid, gbflrid) values($theGiver, $theReceiver)";
mysqli_query($conn, $insertsql);
?>
推荐答案
您可以使用e.preventDefault();
或return false;
<script>
$(document).ready(function(e) {
$('#submitpicks').on('submit',function(e){
e.preventDefault();
$.post('submitpick.php', $(this).serialize(), function(data) {
$('#results').html(data);
});
// return false;
});
});
</script>
注意:在您的php中,您不会回显任何内容以将其作为数据返回..因此,当您尝试使用$ .post或$ .get或$ .ajax ..来检查js和php ..所以在php
Note: in your php you not echo out anything to get it back as a data .. so basic knowledge when you trying to use $.post or $.get or $.ajax .. to check the connection between js and php .. so in php
<?php
echo 'File connected';
?>
然后是js中的alert(data)
..如果一切正常,..转到下一步
and then alert(data)
in js .. if everything works fine .. go to next step
解释每个步骤.
在使用
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
从w3schools网站..它完全错误..您应该寻找如何安装jquery ...然后
from w3schools website.. its totally wrong .. you should looking for how to install jquery ... then
第一次使用js提交表单并防止重新加载..并且您在首页中使用了<script>
1st to submit form with js and prevent reloading.. and you used <script>
in your main page
<script>
$(document).ready(function(e) {
$('#submitpicks').on('submit',function(e){
e.preventDefault(); //this will prevent reloading page
alert('Form submitted Without Reloading');
});
});
<script>
输出:警报,无需重新加载即可提交表单...如果此步骤很好,您会收到警报..转到下一步
output : alert with Form submitted Without Reloading ... if this step is good and you get the alert .. go to next step
第二次将$ .post添加到您的代码中
2nd add $.post to your code
<script>
$(document).ready(function(e) {
$('#submitpicks').on('submit',function(e){
e.preventDefault(); //this will prevent reloading page
$.post('submitpick.php', $(this).serialize(), function(data){
alert(data);
});
});
});
<script>
并在Submitpick.php >>>中,确保您的mainpage.php和Submitpick.php位于同一目录中
and in submitpick.php >>> be sure your mainpage.php and submitpick.php in the same directory
<?php
echo 'File connected';
?>
输出:连接了文件时发出警报
output: alert with File connected
这篇关于jQuery提交PHP未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!