How do I FORMAT a Get Url for Ajax WHERE x=post

0 favourites
  • 7 posts
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • [attachment=0:3lsg8pe6][/attachment:3lsg8pe6]Hi all,

    I am trying to use Ajax to get some data out of the base.

    The bit i am stuck on is in Construct2 where the url to request is

    "http://mysite.com/myphps/getdata.php?name="&textbox1.text [/code:3lsg8pe6]
    
    the result i am looking to compute is 
    $name=post_['name']
    Data from base WHERE name =$name.
    
    All the videos i have seen so far don't have the a variable to play with and I am running out of ideas.
    thanks.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure what exactly your stuck on...

    If it is the php part; post_['name'] doesn't look correct. Try $_POST["name"]

    If that isn't the problem then:

    1. Can you output textbox1.text somewhere to see it is getting the correct text?, you'll need to url encode it so that it doesn't cause problem with spaces and special characters!

    2. Can you post the php you have so far if the problem is there?

  • Not sure what exactly your stuck on...

    If it is the php part; post_['name'] doesn't look correct. Try $_POST["name"]

    If that isn't the problem then:

    1. Can you output textbox1.text somewhere to see it is getting the correct text?, you'll need to url encode it so that it doesn't cause problem with spaces and special characters!

    2. Can you post the php you have so far if the problem is there?

    Hi IndieKiwi, Thanks for helping,

    Heres the php ,

    <?php
    header('Access-Control-Allow-Origin: *');
    
    $host="mysite.com/ect"; // Host name 
    $username="username1"; // Mysql username 
    $password="passwordtest"; // Mysql password 
    $db_name="dbname1"; // Database name 
    $tbl_name="table1"; // Table name
    
    $name = mysqli_real_escape_string($con, $_POST['name']); 
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Retrieve data from database 
    $sql="SELECT * FROM pits WHERE name='$name' ";
    $result=mysql_query($sql);
    
    // Start looping rows in mysql database.
    while($rows=mysql_fetch_array($result)){
    echo $rows['name'] . "|" . $rows['info1'] . "|". $rows['info2'] . "|";
    
    // close while loop 
    }
    
    // close MySQL connection 
    mysql_close();
    ?>[/code:2t4wk5eg]
    When I change this
    [code:2t4wk5eg]$sql="SELECT * FROM pits WHERE name='$name' ";[/code:2t4wk5eg]
    to this
    [code:2t4wk5eg] $sql="SELECT * FROM pits ";[/code
    I get all the data from the base back 
    
    and when i change to 
    [code]$sql="SELECT * FROM pits WHERE name='bob' ";[/code:2t4wk5eg]
    I get bob's data back ,
    
    this leads me to believe that I have screwed up the Construct2 line.
  • I have not worked with PHP for quite a while now, but I doubt that AJAX is sending a POST-Request. I think the correct syntax should be $_GET['name'] instead

  • I have not worked with PHP for quite a while now, but I doubt that AJAX is sending a POST-Request. I think the correct syntax should be $_GET['name'] instead

    Thank you,

    I have changed the php to read

    $name = $_GET['name'];
    $sql = "SELECT * FROM pits WHERE name=$name "; [/code:15gvjrye]
    
    this duz not compute to wanted output. just keep getting a blank page.
    
    However if i change  it to select from a different column like id , 
    [code:15gvjrye]$id = $_GET['id'];
    $sql = "SELECT * FROM pits WHERE id=$id ";[/code:15gvjrye] 
    and use a URL like " mysite.com/myphps/get.php?id=1"
    
    I get all the data from line 1 or 2 or 3 what ever line i put at "?id= "  {it also works with other columns that hold numbers for data } but not any of the text columns,
    
    is there a setting to stop the php reading and searching text columns ?
  • I imagine your column "name" is string and "id" is numeric?

    In this case, try this:

    $name = $_GET['name'];
    $sql = "SELECT * FROM pits WHERE name='$name'";
    [/code:e6xf06by]
  • I imagine your column "name" is string and "id" is numeric?

    In this case, try this:

    > $name = $_GET['name'];
    $sql = "SELECT * FROM pits WHERE name='$name'";
    [/code:196bkfxi]
    

    WONDERFULL Ubivis , thank you very much , and thank you again,

    -------------------------------------------------------

    Ajax OUT working

    -------------------------------------------------------

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)