[英]Passing a javascript variable to a php switch case statement
The javascript parameter "Step" should trigger a switch-case function in php. javascript参数“ Step”应触发php中的切换大小写功能。 If Step is one than trigger this piece of code in php and return the output by JSON.
如果Step是一个触发器,则在php中触发这段代码并通过JSON返回输出。
If I take a look in firebug the post string is: Step=one&inputFname=rick&inputLname=bovenkamp
I think this is correct. 如果我看一下萤火虫,发布字符串是:
Step=one&inputFname=rick&inputLname=bovenkamp
我认为这是正确的。 So the problem must be in the php file and I think it's in the $_POST
part... 所以问题一定在php文件中,我认为它在
$_POST
部分中...
What am I doing wrong? 我究竟做错了什么? Any help would be very great!
任何帮助都将非常棒!
javascript code: JavaScript代码:
$(document).ready(function() {
$("form#userForm").submit(function() {
var inputFname = $('#inputFname').attr('value');
var inputLname = $('#inputLname').attr('value');
var Step = "one";
$.ajax({
type: "POST",
url: "main.php",
data: {Step: Step,inputFname: inputFname,inputLname: inputLname},
dataType: "json",
contentType:"application/json; charset=utf-8",
success: function(data) {
$("p.succesText").html(data.jsCode);
$("form#userForm").hide();
$("div.success").fadeIn();
},
error: function(xhr, status, error) {
$("form#userForm").hide();
$("p.errorHead").html("Something went wrong.");
$("p.errorText").text("ResponseText: " + xhr.responseText
+ "Statuscode: " + xhr.status
+ "ReadyState: " + xhr.readyState);
$("div.error").fadeIn();
}
});
return false;
});
});
PHP file: PHP文件:
<?php header('content-type: application/json; charset=utf-8');
$log = array();
$varStep = htmlspecialchars(trim($_POST["Step"]));
switch($varStep) {
case "one":
$varFname = htmlspecialchars($_POST["inputFname"]);
$varLname = htmlspecialchars($_POST["inputLname"]);
//Make Database connection
$db = mysql_connect("192.168.178.254","root","852456");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("Ajax" ,$db);
//Generate code and check if code already exists in the database
do
{
$varCode = rand(10000, 99999);
$dbCheckCode = "";
$dbCheckCode = mysql_query("SELECT * FROM TableAjax WHERE code='$varCode'");
}
while (mysql_fetch_array($dbCheckCode) !== false);
//Save the Form data in the database
$sql = "INSERT INTO TableAjax (fname, lname, code) VALUES (".PrepSQL($varFname) . ", " .PrepSQL($varLname) . ", " .PrepSQL($varCode) . ")";
mysql_query($sql);
//Return code to frontend
$log['jsCode'] = $varCode;
break;
}
echo json_encode($log);
//Clean SQL statement
function PrepSQL($value)
{
if(get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
Put Step in quotes data : {"Step" : Step,...
. 将Step放入报价
data : {"Step" : Step,...
You are passing the value of the variable as the key in that case, that is to say you are actually passing data : {"one" : "one",...
. 在这种情况下,您将传递变量的值作为键,也就是说,您实际上正在传递
data : {"one" : "one",...
You should do the same with inputLname
and inputFname
. 您应该对
inputLname
和inputFname
进行相同的inputFname
。
Edit - Explanation 编辑-说明
If you look at what the contentType options does here at http://api.jquery.com/jQuery.ajax/ , you will see that the default is application/x-www-form-urlencoded
which is what you want. 如果您在http://api.jquery.com/jQuery.ajax/上查看contentType选项的作用,则会看到默认值是
application/x-www-form-urlencoded
,这是您想要的。 Essentially what your PHP error was indicating is that the $_POST
array was empty because it did not know how to read your data due to the format. 本质上,PHP错误指示的是
$_POST
数组为空,因为由于格式,它不知道如何读取数据。 You want your return data to be json
, the dataType
option was all you needed. 您希望返回的数据为
json
,仅需要dataType
选项。
You still would have needed to do what I indicated in the first part of the post, but essentially you had two errors that were tripping you up. 您仍然需要按照我在文章的第一部分中指出的做事,但是从本质上讲,您有两个错误使您绊倒。
I hope this makes sense! 我希望这是有道理的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.