[英]how to run php code on submit button without refreshing/ reloading the page
我想在提交按鈕單擊時執行一些php代碼,而不刷新/重新加載我的頁面。 可能嗎? 我在頁面加載時也具有javascript函數,這就是為什么我不想刷新頁面。 提前致謝。
<?php
if(isset($_POST["search"]))
{
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
$rowID=1;
while($row = mysql_fetch_array($rs))
{
echo "<tr>";
echo "<td width='130px' id='name.$rowID.'>$row[Name]</td>";
echo "<td width='230px' id='link.$rowID.'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px' onclick='Display($rowID);'>$add_to_textbox</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
#**********************
mysql_free_result($rs);
}
?>
<script type="text/javascript">
function Display(rowID){
var linkVal = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n");
document.getElementById("name").value = document.getElementById('name'+rowID+'').innerHTML;
document.getElementById("link").value = linkVal;
}
</script>
這是我的代碼
好吧,您需要使用javascript / ajax。
示例:在您的提交鏈接(例如a href)上,添加調用js的函數submitMe
並傳遞所需的任何變量
function submitMe() {
jQuery(function($) {
$.ajax( {
url : "some_php_page.php?action=getsession",
type : "GET",
success : function(data) {
alert ("works!"); //or use data string to show something else
}
});
});
}
如果要動態更改某些內容,這很容易-您只需創建標簽並為其分配ID: <div id="Dynamic"> </div>
然后,您可以使用來加載這兩個標簽之間的任何內容
document.getElementById("Dynamic").innerHTML = "<b>BOOM!</b>";
這意味着您調用了兩個標簽之間的區域,並在其中加載了一些內容。 從該位置獲取數據的方式相同:
alert(document.getElementById("Dynamic").innerHTML);
請閱讀以下內容: http : //www.tizag.com/javascriptT/javascript-getelementbyid.php
此外,還可以玩DOM元素並進行實驗,並了解它們如何相互作用。 這並不難,只需花費一些時間即可掌握所有概念。
每當您從html表單發送請求ajax(無論如何都使用純js)時,請確保添加return false語句以防止重定向:類似於:
<form method="post" ... onsubmit="ajax_post(); return false;">
您必須使用ajax,但是您可以使用普通javascript(無需jquery)進行操作。 jQuery使它變得更容易。
普通的javascript示例:此函數將通過get觸發ajax,不帶參數:您可以對其進行調整,使其在POST中運行並能夠發送一些參數: file表示要請求的php文件, html表示數據將要使用的容器顯示:
function plain_ajax(file,html){
if (window.XMLHttpRequest){
r=new XMLHttpRequest(); //For Most BRowser
} else{
r=new ActiveXObject("Microsoft.XMLHTTP"); //for Explorer
}
//When the state change
r.onreadystatechange=function(){
//ReadyState 4 = Loaded see: http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/
//status 200 = ok see: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if(r.readyState==4 && r.status==200){
//Replace the content with the response, Using creatDocumentFragment is more efficient
//at the cost of a few more lines But it would alos allow you to append the data
//instead of replacing it using innerHTML;
document.getElementById(html).innerHTML = r.responseText;
}
}
//Opening the connection and sending the request.
r.open("GET",file,true);
r.send();
}
您的HTML或PHP表單
<form action="submit.php" method="post">
Name: <input name="name" id="name" type="text" /><br />
Email: <input name="email" id="email" type="text" /><br />
Phone Number: <input name="phone" id="phone" type="text" /><br />
<input type="button" id="searchForm" onclick="SubmitForm();" value="Send" />
</form>
您的JavaScript
<script>
function SubmitForm() {
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
$.post("submit.php", { name: name, email: email, phone: phone },
function(data) {
alert("Data Loaded: " + data);
});
}
</script>
您的PHP頁面要做一些活動
<?php
echo $_POST['name']."<br />";
echo $_POST['email']."<br />";
echo $_POST['phone']."<br />";
echo "All Data Submitted Sucessfully!"
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.