0

So, not entirely sure if this is correct, but, here is my php doc: connect.php

  <?php
mysql_connect("timereliefusers.db.8956371.hostedresource.com", "timereliefusers", "User1!db") or die(mysql_error());
mysql_select_db("timereliefusers") or die (mysql_error());


    $choice =(int) $_GET["choice"];

    switch ($choice) {
        case 1:
        mysql_query("UPDATE poll1 set choice1 = choice1 + 1");
        break;
        case 2:
        mysql_query("UPDATE poll1 set choice2 = choice2 + 1");
        break;
        case 3:
        mysql_query("UPDATE poll3 set choice3 = choice3 + 1");
        break;
    }
}

?>

Then, I send an AJAX request to the php doc here:

$(document).ready(function(){       
    var choice = 0;

    $("#s1main div").click(function(){
        $("#s1main div").removeClass("buttonclicked");
        $(this).addClass("buttonclicked");

        if ($(this).attr("id") == "choice1"){
            choice = 1;
        }

        if ($(this).attr("id") == "choice2"){
            choice = 2;
        }

        if ($(this).attr("id") == "choice3"){
            choice = 3;
        }

        xmlhttp.open("GET", "connect.php?choice=" + choice, true);
        xmlhttp.send();
        alert("Variables passed");          

    });

Please tell me of any errors concerning these two documents, thank you - Chris

NOTE: This doesn't get past the alert("variables passed"); for some odd reason, could someone explain as to why that happens? There must be something wrong with the AJAX call.

4
  • 2
    You might want to remove your username and password for the DB from this so others can't see it Commented Feb 23, 2012 at 12:49
  • 5
    Assuming you're using jQuery in your js script, why don't you use $.get or $.ajax instead of xmlhttp? Commented Feb 23, 2012 at 12:51
  • xmlhttp does the exact same thing, if I were to use $.get, how would I be passing the variable? Commented Feb 23, 2012 at 13:15
  • @ChrisCates what do you mean never gets passed the alert? That is the last thing in your script... Is it never alerting? Commented Feb 23, 2012 at 22:37

1 Answer 1

1

Looks like you never send choice to your script...you should do something like this:

PHP:

<?php

mysql_connect("db.host", "username", "password") or die(mysql_error());
mysql_select_db("timereliefusers") or die (mysql_error());


$choice = (int) $_GET["choice"];

switch ($choice) {
    case 1:
    mysql_query("UPDATE poll1 set choice1 = choice1 + 1");
    break;
    case 2:
    mysql_query("UPDATE poll1 set choice2 = choice2 + 1");
    break;
    case 3:
    mysql_query("UPDATE poll3 set choice3 = choice3 + 1");
    break;
}
?>

JAVASCRIPT

$("#next").click(function(){
    if (choice != 0) {
            xmlhttp.open("GET", "connect.php?choice=" + choice, true);
            xmlhttp.send();
        }
    });
Sign up to request clarification or add additional context in comments.

4 Comments

notice $choice = (int) $choice this will keep you from having someone sneak a string by, I would have usually mysql_real_escape_stiring() but since its just numbers (int) will work fine. Also, notice the javascript, "connect.php?choice=" + choice
Could you edit out his connection info please - he's done it in the main post now so it's just left on display here :)
Great thanks, testing it on my server now. Knew I was missing something when passing variables, this should do the trick.
I did some workarounds, take a look at the original post to check for any errors. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.