ajax post to php & sql local host database

0 favourites
  • 2 posts
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • 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

    <?php

    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******

    ***CONSTRUCT2***

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

    "fname='lance'&input='" & chatinput.text & "'"

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    "fname=lance&input=" & chatinput.text[/code:p4op1j99]
    
    [code:p4op1j99] On AJAX completed --> browser.log AJAX.lastData[/code:p4op1j99] 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)
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)