How do I perform a simple ajax post

Get help using Construct 2

Post » Mon May 30, 2016 11:38 am

Hello, I've managed to make the Get with ajax to work, now i'm trying to Post some data and I'm getting errors.
Here is how my php file looks like:
Code: Select all
header("Access-Control-Allow-Origin: *");

$user = "root";
$pass = "";

$dbh = new PDO('mysql:host=localhost;dbname=dbname', $user, $pass);

$Login = $_POST['L'];
$PW = $_POST['S'];
$datum = '{"c2array":true,"size":[3,10,1],"data":[[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]],[["Fatso"],[1000],[100],[900],[10],[10],[39],[10],[10],[0]],[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]]]}';


$sql = $dbh->query("UPDATE registration SET data = '$datum' WHERE uemail = '$Login' AND upassword = '$PW'");


Here is my construct 2 post request:
Image

At the debugger i'm getting:
Code: Select all
Undefined index: L
B
11
S
3
Posts: 44
Reputation: 713

Post » Mon May 30, 2016 1:02 pm

It seems like what you're trying to do is sending data twice (two datas – first in URL field and another time in Data field).

Try to do like this:
URL: "http://localhost/legacy/save.php"
Data: "L=" & UserLogin & "&S=" & UserPass

I'm not sure what's the savestats.Text inside the Data field.
Last edited by grigrizljac on Mon May 30, 2016 1:13 pm, edited 1 time in total.
B
32
S
7
G
4
Posts: 398
Reputation: 4,591

Post » Mon May 30, 2016 1:10 pm

grigrizljac wrote:It seems like what you're trying to do is sending data twice (two datas – first in URL field and another time in Data field).

Try to do like this:
URL: "http://localhost/legacy/save.php"
Data: "L=" & UserLogin & "&S=" & UserPass"

I'm not sure what's the savestats.Text inside the Data field.


Thanks for reply, savestats.Text contains json string that must be inserted into the database.
In other words, the place with $datum, must be provided with the string from savestats.Text
B
11
S
3
Posts: 44
Reputation: 713

Post » Mon May 30, 2016 1:14 pm

Then, I guess, you should add another parameter to the php and to c2
Example: if you add a parameter $string to php, make Data in c2 to: "L=" & UserLogin & "&S=" & UserPass & "&string=" & savestats.Text
B
32
S
7
G
4
Posts: 398
Reputation: 4,591

Post » Mon May 30, 2016 1:19 pm

grigrizljac wrote:Then, I guess, you should add another parameter to the php and to c2
Example: if you add a parameter $string to php, make Data in c2 to: "L=" & UserLogin & "&S=" & UserPass & "&string=" & savestats.Text

Thanks a lot! That kept me occupied for several hours.

Here is how I solved it:
Undefined index: has been resolved using isset:
Code: Select all
if(isset($_POST['L'])){
    $Login = $_POST['L'];

}else {
    $Login = null;

}


if(isset($_POST['S'])){
    $PW = $_POST['S'];

}else {
    $PW = null;

}



if(isset($_POST['D'])){
    $datum = $_POST['D'];

}else {
    $datum = null;

}


and at construct 2 post I've used the following path on the basis of your suggestion:
Code: Select all
"L=" & UserLogin & "&S=" & UserPass & "&D=" & savestats.Text


Working like a charm!
B
11
S
3
Posts: 44
Reputation: 713


Return to How do I....?

Who is online

Users browsing this forum: Lancifer, MPPlantOfficial, Sup with that? and 62 guests