Lance
Lance

Reputation: 4820

Retrieve info sent to php file with jquery .get

I have this function that send data to a php file, but for some reason, it seems like the php file isn't getting the data that jquery sent to it.

Here is the jquery code:

<script type="text/javascript" src="JS/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".hide").hide();
        $("#cake").hide();

        var boz = $('#cake').val();
        var search = $('#search').val();

    $.get("petition_send.php", { id: boz, q: search }, function(data){
        alert("Data Loaded: " + data);
     });

    $('.show').click(function checkPass(){
        $(".hide").slideDown();
    });

    });
</script>

Now, here is the php file:

<?php
$xmlDoc = new DOMDocument();

include_once("petitionhint.php");

$xmlDoc->loadXML($xmlout);

$x=$xmlDoc->getElementsByTagName('friend');

//Get the q parameter from URL
$q      =   $_GET["q"];
$id     =   $_GET["petition_ID"];

//Lookup all links from the xml file if length of q>0
if(strlen($q)>0)
{
    $hint = "";

    for($i=0; $i<($x->length); $i++)
    {
        $y=$x->item($i)->getElementsByTagName('name');
        $z=$x->item($i)->getElementsByTagName('url');
        $w=$x->item($i)->getElementsByTagName('photo');
        $l=$x->item($i)->getElementsByTagName('location');

        if ($y->item(0)->nodeType==1)
        {
            //Find a link matching the search text
            if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
            {
                if($hint != ""){
                        $hint .= "<br />";
                   }
                    $hint .= "<h1 id='poli'><a id='blue' href='";
                    $hint .= $z->item(0)->childNodes->item(0)->nodeValue;
                    $hint .= "&amp;petition_ID='$id' >";
                    $hint .= $y->item(0)->childNodes->item(0)->nodeValue;
                    $hint .= "</a> <br /><a id='green'>";
                    $hint .= $l->item(0)->childNodes->item(0)->nodeValue;
                    $hint .= "</a>";
                    $hint .= "<br/><img width='30' height='30' id='schmidt' src='features/";
                    $hint .= $w->item(0)->childNodes->item(0)->nodeValue;
                    $hint .= "' /></h1>";
            }
        }
    }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if($hint == "")
{
    $response = "No suggestions";
} else
{
    $response   =   $hint;
}

//output the response
echo $response;
?>

When I try in include the "$id" variable into the part of the php code here:

$hint .= "&amp;petition_ID=$id' >";

it doesn't appear. the $id variable doesn't appear.

Upvotes: 0

Views: 206

Answers (2)

shevski
shevski

Reputation: 1022

You don't select the proper element from $_GET:

$id     =   $_GET["petition_ID"]; 

to

$id     =   $_GET["id"]; 

and

$hint .= "&amp;petition_ID='$id' >"; 

to

$hint .= "&amp;petition_ID=$id' >"; 

also try in jquery call

    $.get("petition_send.php", { "id": boz, "q": search }, function(data){ 
    alert("Data Loaded: " + data);

or consider changing "id" to something else.

Upvotes: 2

AndreKR
AndreKR

Reputation: 33707

You are sending the parameters id(boz) and q(search) but you are reading the parameters q and petition_ID.

Upvotes: 2

Related Questions