使用上面的代码,我的用户名变量没有正确传递,我假设我编码datastring参数的方式有问题,但我不知道如何正确地做到这一点。
下面是我在signinout.php中使用的php代码,用于将用户名插入数据库,没有输入用户名字段,每个新条目都进入数据库。
$username = protect($_POST['username']); $time = time(); $sql = "INSERT INTO users (username, join_date) VALUES ('$username', '$time')"; $result = mysqli_query($cn, $sql) or die(mysqli_error($cn)); var username = $('#username').val(); var dataString = 'username=' + username; $.ajax({ type: "POST", url: "signinout.php", data: dataString, success: function() { $('.user').html('<span>Welcome <span id="loggedUser">' + username + '</span>!</span> <a id="signOut" onclick="window.location.reload()">SIGN OUT</a>'); } });using the above code, my username variable is not being passed on correctly, I'm assuming something is wrong with the way I coding the datastring parameter but I'm not sure how to do it correctly.
Below is the php code that I am using in signinout.php to insert the username into the database, the username field is not being entered with each new entry into the database.
$username = protect($_POST['username']); $time = time(); $sql = "INSERT INTO users (username, join_date) VALUES ('$username', '$time')"; $result = mysqli_query($cn, $sql) or die(mysqli_error($cn));最满意答案
您的“最佳”数据字符串取决于您在服务器端部分的需求。 例如,这个jquery-ajax调用将一个对象发送到服务器端操作(PHP):
var mydata = null; mydata = "hellostring=1"; mydata = { title: "some" , value: "thing" }; mydata = [1,2,3]; $.ajax({ cache: false, type: 'post', async: true, data: mydata, url: 'some-script.php', success: function(resp){ console.log("OK",resp); }, error: function(e){ console.log(e.responseText); } });因此,在您的服务方面,您可能拥有此脚本,该脚本将返回与您发送的相同:
// some-script.php <?php echo print_r($_POST,true); ?>每种数据的输出(参见mydata变量)是:
案例 :mydata =“hellostring = 1”;
Array( [hellostring] => "1" )这意味着,在服务器端,您可以:
$_123 = $_POST["hellostring"];Case mydata = {title:“some”,value:“thing”};
结果,你得到:
Array ( [title] => some [value] => thing )所以你可以:
$title = $_POST['title']; $value = $_POST['value'];Case mydata = [1,2,3];
惊喜,这不起作用,:),你应该用这种形式包装它:
mydata = {some:[1,2,3]}
因此,您可以像上一个案例一样在服务器端继续。
注意:
为避免被黑客攻击:( PHP CASE示例)使用以下方法过滤输入: http://php.net/manual/es/function.filter-input.php
更多
为了在服务器端部件中进行更高级的数据处理(即:在接收ajax请求的脚本中),您可以通过以下方式使用json:
首先假设你是通过javascript发送一个对象:
// in your client part, mydata = { title: "some" , value: "thing", mydog: "sammy" }; ..do your ajax call stuff here..并且,在您的服务器端:
<?php // some-script.php $obj = json_decode(file_get_contents('php://input')); echo $obj->title; // output: "some" echo $obj->value; // output: "thing" echo $obj->mydog; // output: "sammy" ?>Your "best" datastring depends on your needs in the server side part. As an example, this jquery-ajax call send a object to a server side action (PHP) :
var mydata = null; mydata = "hellostring=1"; mydata = { title: "some" , value: "thing" }; mydata = [1,2,3]; $.ajax({ cache: false, type: 'post', async: true, data: mydata, url: 'some-script.php', success: function(resp){ console.log("OK",resp); }, error: function(e){ console.log(e.responseText); } });As result, in your serve side you may have this script, which will return the same as you send:
// some-script.php <?php echo print_r($_POST,true); ?>The outputs, for each kind of data (see the mydata variable) is:
Case: mydata = "hellostring=1";
Array( [hellostring] => "1" )this mean, in serverside, you can:
$_123 = $_POST["hellostring"];Case mydata = { title: "some" , value: "thing" };
As result, you get:
Array ( [title] => some [value] => thing )So you can:
$title = $_POST['title']; $value = $_POST['value'];Case mydata = [1,2,3];
Surprise, this doesnt work, :) , you should wrap it, in this form:
mydata = { some : [1,2,3] }
So, you can proceed in your server-side the same as the previous case.
Note:
To avoid get hacked: (PHP CASE example) filter your input using: http://php.net/manual/es/function.filter-input.php
More
In order to have a more advanced data handling in your server side part, (that is: in the script who receive the ajax request) , you can make usage of json, in this way:
Let start by supposing you are sending a object via javascript:
// in your client part, mydata = { title: "some" , value: "thing", mydog: "sammy" }; ..do your ajax call stuff here..And, in your server side:
<?php // some-script.php $obj = json_decode(file_get_contents('php://input')); echo $obj->title; // output: "some" echo $obj->value; // output: "thing" echo $obj->mydog; // output: "sammy" ?>更多推荐
发布评论