[英]Can't get Jquery value on php side
I have an array, it is filled in javascript code. 我有一个数组,它填充了javascript代码。 I need to reach the values of this array on php side when confirm button clicked.
当确认按钮单击时,我需要在php端达到此数组的值。 So, I am using ajax post method as below.It is running successfully, I know because all of the alerts are working successfully.
因此,我正在使用ajax post方法,如下所示。它成功运行,我知道是因为所有警报都成功运行。 This is js part of my code.
这是我代码的js部分。
$('#firstConfirmButton').click(function(){
var my_json_val = JSON.stringify(ordersArray, null);
alert(my_json_val.toString());
$.ajax({
type: "POST",
url: "test.php",
data: my_json_val,
cache: false,
contentType: false,
async: false,
processData: false,
success: function(response){
//alert("---"+data);
alert("Settings has been updated successfully.");
alert(my_json_val);
alert(response);
window.location.reload(true);
}
});
});
On the other side, I have a test.php page and also it contains "confirm" button that use the js code above. 另一方面,我有一个test.php页面,它还包含使用上述js代码的“确认”按钮。 When this button clicked, I want to echo values of the array that I posted with ajax.
单击此按钮后,我想回显我用ajax发布的数组的值。 But I can not get the values on php side.
但是我无法获取PHP端的值。 Here is my php code;
这是我的php代码;
<?php
require("includes/db.php");
require("includes/functions.php");
session_start();
if (isset($_POST['firstConfirmButton'])) {
echo 'I am here'; //I can see only this row on php page
$my_json_encoded = $_POST['data'];
$my_json_val = json_decode($my_json_encoded);
echo $my_json_val; //nothing happens
}
and here is my html code for confirm button; 这是我的用于确认按钮的html代码;
<form role="form" method="post" action="starbucks.php">
<div id="firstConfirmButton">
<button type="submit" name="firstConfirmButton" id="firstConfirmButton" class="btn btn-primary btn-lg">Onayla</button>
</div>
So where is my fault, please help! 所以我的错在哪里,请帮忙!
不要将数组转换为Json,Ajax my_json_val
自己转换,因此将my_json_val
替换为ordersArray
。
Do you really want to redirect to your php file or do you just need data from it? 您真的要重定向到您的php文件还是只需要其中的数据? Because action="file.php" redirects to this file.
因为action =“ file.php”重定向到此文件。 If you just need data, try to use jquerys .submit method and do an ajax call, otherwise use it to remaster your action attribute (for simplicity i would use an id on the form if you have multiple forms on your site):
如果您只需要数据,请尝试使用jquerys .submit方法并进行ajax调用,否则请使用它来重新生成您的action属性(为简单起见,如果您的站点上有多个表单,我将在表单上使用id):
$('#yourform').submit(function(){
var my_json_val = JSON.stringify(ordersArray, null);
var elem = {
firstConfirmButton: true,
data: my_json_val
}
$.ajax({
type: "POST",
url: "test.php",
data: elem,
cache: false,
contentType: false,
async: false,
processData: false,
success: function(response){
//alert("---"+data);
alert("Settings has been updated successfully.");
alert(my_json_val);
alert(response);
}
});
return false;
});
if you actually want to redirect, try using this: 如果您确实要重定向,请尝试使用以下命令:
$('#yourform').submit(function(ev){
var my_json_val = JSON.stringify(ordersArray, null);
$(this).attr('action', ev.target.action+'?data="'+my_json_val+'"');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.