2016-10-02 26 views
0

我试图提交使用与jQuery Mobile的AJAX形式没有它刷新页面和我有没有运气..AJAX形式要求仍然重新加载页面

我有这样的形式 -

的index.php:

<script> 
function SubmitForm() { 
var name = $("#name").val(); 
$.post("bump.php", { name: name}, 
function(data) { 
//alert(data); 
}); 
} 
</script> 


<form method="post" data-ajax="false"> 
<input name="name" type="hidden" id="name" type="text" value="<?php echo $id; ?>" /> 
<input type="image" style="height:35px;top:4px;" id="bump" src="../img/bump.png" id="searchForm" onclick="SubmitForm();" value="Send" /> 
</form> 

这里是bump.php

$date = date('y/m/d H:i:s'); 
$id = $_POST['name']; 
$sql = "UPDATE images SET update_date='$date' WHERE id=$id"; 

    if ($conn->query($sql) === TRUE) { 

    } else { 
     echo "Error updating record: " . $conn->error; 
    } 

我想保持我的位置页面但每次刷新?我在这里做错了什么?

+0

您可以使用此链接可能对您有所帮助 [输入链接描述](http://stackoverflow.com/questions/12944329/add-onclick-function-to-a-submit-button) –

+0

可能对你有所帮助 [将onclick函数添加到提交按钮](http://stackoverflow.com/questions/12944329/add-onclick-function-to-a-submit-button) –

回答

3

使用event.preventDefault();取消事件,如果它是可取消的,而不停止事件的进一步传播。的type="image"

默认行为是提交form因此页面是unloaded

function SubmitForm(event) { 
 
    event.preventDefault(); 
 
    var name = $("#name").val(); 
 
    $.post("bump.php", { 
 
     name: name 
 
    }, 
 
    function(data) { 
 
     //alert(data); 
 
    }); 
 
}
<form method="post" data-ajax="false"> 
 
    <input name="name" type="hidden" id="name" type="text" value="<?php echo $id; ?>" /> 
 
    <input type="image" style="height:35px;top:4px;" id="bump" src="../img/bump.png" id="searchForm" onclick="SubmitForm(event);" value="Send" /> 
 
</form>

+0

Ahh完美谢谢! @Rayon必须等待8分钟,接受 –

+0

Haa go figure ..继承人追加加分,当表单提交隐藏字段时,它插入了正确的id,但是当我在bump.php中回显id时, ID中最近的ID不是在隐藏字段中插入的ID ..非常奇怪..再次感谢! –

0

用户onclick`

function SubmitForm() { 
 
//event.preventDefault(); 
 
var name = $("#name").val(); 
 
console.log(name); 
 
$.post("message.html", { name: name}, 
 
function(data) { 
 
alert(data); 
 
}); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<form method="post" data-ajax="false"> 
 
<input name="name" type="hidden" id="name" type="text" value="<?php echo $id; ?>" /> 
 
<input type="image" style="height:35px;top:4px;" id="bump" src="http://switchon.global2.vic.edu.au/files/2015/07/4x5-1jroh2i.png" id="searchForm" onclick="SubmitForm(); return false;" value="Send" /> 
 
</form>
返回false

相关问题