[英]Pass Jquery variable to PHP variable in same page
我試圖在按下按鈕.bid(這是一個提交按鈕)時,將Jquery變量(項目)傳遞給同一頁面上的php變量$ itemdestination:
<script>
$(".bid").click(function () {
var items = $(this).closest("tr") // Finds the closest row <tr>
.find(".nr") // Gets a descendent with class="nr"
.text(); // Retrieves the text within <td>
var itemdestination = $(this).closest("tr") // Finds the closest row <tr>
.find(".no") // Gets a descendent with class="nr"
.text();
$("#prøve").append(items, itemdestination); // Outputs the answer
$.ajax({
url: 'Jobs.php',
type: 'POST',
data: {items: items}
});
});
</script>
<?php
if (isset($_POST['button_pressed'])) {
$itemdestination = $_POST['items'];
//Email information
$admin_email = "admin@speditionhub.com";
$email = $userName;
$subject = "Bid registered on your cargo";
$comment = $itemdestination;
echo $admin_email;
echo $comment;
//send email
mail($email, "$subject", $comment, "From:" . $admin_email);
}
?>
<script>
alert("Your bid is registered");
</script>
<?php
}
?>
但是我使用的方法沒有傳遞任何東西。 我究竟做錯了什么??
刪除以下if語句,然后嘗試:
if(isset($_POST['button_pressed']))
這不是嗎?
if(isset($_POST['button_pressed']))
應該:
if(isset($_POST['items']))
似乎您正在檢查錯誤的發布變量。
您需要在js中為button_pressed設置一個post字段(以使您的php運行),然后對返回的數據進行實際處理:
$.ajax({
url: 'Jobs.php',
type: 'POST',
data: {items: items, button_pressed: true},
success: function(data){
console.log(data);//do something with the data returned
}
});
如果這綁定到提交按鈕上的單擊事件,則進行編輯,必須使用event.preventDefault停止正常提交的表單:
$(".bid").click(function (ev) {
ev.preventDefault();
//the rest of your code here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.