passing PHP parameter to Ajax function ...Pls help!

Discussion in 'JavaScript' started by jasso, Aug 26, 2009.

  1. #1
    I have $x =10; and a radio button as an input.
    when clicked, the Ajax function "getVote" will be called.And in the call, variable $x and value of the input radio button (either '0' or '1')) will be passed as arguments.

    The ajax function will pass these values to show.php where they will be echoed.

    The problem:

    It (ajax) can pass the value of the radio button to Show.php but the other php variable ($x) is passed as blank or ""... how to make it to pass the value of $x ?:confused:

    The following is my code.

    Example:

    PHP/HTML:

    <?php echo "YES"; ?>: <input type="radio" name="vote" value="1" onclick="getVote(this.value,'<?php echo $x; ?>')" />


    Ajax:


    function getVote(int,xval)
    {
    if (str.length==0)
    {
    document.getElementById("Answer").innerHTML="";
    return;
    }

    var xmlHttp;
    try {
    xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
    try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
    try {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
    alert("Your browser does not support AJAX!");
    return false;
    }
    }
    }
    xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
    document.getElementById("Answer").innerHTML=xmlHttp.responseText;
    }
    }
    var url="show.php";
    url=url+"?vote="+int;
    url=url+"&x="+xval;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }


    PHP file (show.php)

    <?php
    $xvote = $_REQUEST['vote'];
    $xvote = $_REQUEST['x'];

    echo $xvote;
    echo $x;
    ?>
     
    jasso, Aug 26, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    While this could be a hell of a lot simpler in jQuery, I just have to ask:

    in the bold... where did 'str' come from?
     
    kblessinggr, Aug 26, 2009 IP
  3. jasso

    jasso Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Yes, you are right. The bolded part should not be included in the function. But still, the two arguments I am passing are both integer type and I could not figure out how to pass them to the Ajax function and then to the show.php . could you please give some idea as to what would be the best approach...?

    Thanks in advance.
     
    jasso, Aug 26, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well the bolded part if it were still there, would be true and as such would have exited out of the function prior to being able to call the PHP. So removing that, the code should have worked.

    By the way this is how you would do it with JQuery if you're curious. (I'm using xint instead of int because I think 'int' is a reserved keyword in javascript)

    
    <html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
    	function getVote(xint, xval)
    	{
    	    $('#Answer').load("show.php" , "vote="+xint+"&x="+xval);	    
    	}
        </script>
    ...
    
    Code (markup):
    If you did this instead, the values would be sent via the POST method instead of GET (Data passed as a query string sent as GET, data passed as key-value pairs gets sent as POST)
    
    $('#Answer').load("show.php", { vote: xint, x: xval });
    
    Code (markup):
     
    kblessinggr, Aug 26, 2009 IP
  5. jasso

    jasso Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice. I am sort of getting it. The fact is:

    In event.php I am trying to capture and pass the eventID ($x) and the vote (radio button value... 'this.value') to the ajax 'onclick' function 'getvote' as arguments.
    Ajax is sending these arguments to show.php where they will be inserted into mySQL database and the final result of voting will be queried and echoed.

    As because I will have different eventIDs and votes for different events, I will need to pass the eventID with the vote to update the database. and query the database to display result only for the event the user voted for.


    In this scenario can Jquery be used? :confused:
     
    jasso, Aug 26, 2009 IP