ajax post to php & sql local host database

Discussion and feedback on Construct 2

Post » Mon Aug 07, 2017 3:02 pm

Hi all,
Really need some help here. I dont know ajax myself, Iv been researching through the topics here on the site and youtube to get some help. I have created a php file for database connection and variables set up to hold the POST values from construct. The event page of construct is sending a name called 'lance' and the users input into my textbox called 'chatinput'. It all looks ok, but yet nothing is being inserted into the databse when I run the game no matter what Iv tried. There is a connection being made from the php file to my database as Iv tested that, but it seems to be an issue with the ajax part of either sending the values to the php file properly, or the php file isnt picking them up properly??
I am so lost and this game is part of a project Im trying to do for college so Im very stuck at the moment.
Any help would be very appreciated.
Code is below:
gameconnect.php file
header('Access-Control-Allow-Origin: *');
require("db_connect.php"); // call to connection file
include("functions.php");// include funtions file

$db_link = db_connect("game"); //select database
connect($db_link); //call to connect to db function

$fn=$_POST['fname']; //variable for holding fname' lance' from consrtruct2
$answer =$_POST['input']; // variable for user input text

mysql_query("INSERT INTO useranswers(pps, text) VALUES ('$fn', '$answer')");

?> //******end of php file******

parameters for AJAX POST to url section, in the data field this is the instruction I have:

"fname='lance'&input='" & chatinput.text & "'"
Posts: 5
Reputation: 332

Post » Tue Aug 08, 2017 4:37 pm

Not sure if that´s the issue but I think you don´t need the ' things and they might be the problem already. Try this.

Code: Select all
"fname=lance&input=" & chatinput.text

Code: Select all
 On AJAX completed --> browser.log AJAX.lastData
will give you the server response. Maybe there´s a hint there. Maybe you can try calling this from a browser (not sure if you have to use GET in this case instead of POST) and see if that works.

Also I recommend using mysqli instead of mysql, escape user intput (mysql_real_escape_string) and use prepared statements. (It´s sort of a pain but once you understand how everything works it´s no big hassle)
Posts: 69
Reputation: 1,048

Return to Construct 2 General

Who is online

Users browsing this forum: Tokinsom and 8 guests