DCJones
DCJones

Reputation: 3461

Passing a PHP data string to Ajax

I am trying to pass a PHP variable to Ajax which can then be used to return a record/data from a MySQL table.

Background I have a PHP script that returns data and displays it in rows and columns

if($Count2 < $Limit2) {

    if($Coun2 == 0) {
    echo "<tr>";
    }
    echo "

    <td  class='roomviewred' title='View reported fault' id='room'  >
        <span class='roomlinktext' >
            <div class='content1'>
               $RoomNo
            </div>
        </span>
    </td>
    ";
    } else {
    $Count = 0;
    echo "
    </tr>

    <tr>
    <td  class='roomviewred' title='View reported fault' id='room'>
        <span class='roomlinktext'>
            <div class='content1'>
               $RoomNo
            </div>
        </span>
    </td>
    ";
    }
    $Count++;
}

$RoomNo contains a string of data as a 4 digit number. what I need to do is, when either the displayed number of even the containing DIV is clicked send the data to a jquery/ajax function to send a MySQL request to the data table and return the record associated with the query string.

My jquery/ajax function

$(document).ready(function() {

var phproom = <?php echo $RoomNo;?>


new jBox('Modal', {
attach: '.roomviewred',
title: 'Reported fault',
theme: 'TooltipBorder',
closeOnClick: 'body',

ajax: {
    url: 'fault_detail.php',
    data: {
       Room: 'phproom'
    },
    reload: 'strict'
    }
});
}

The issue I have is when the the on screen room number is click my script is not sending the data to the ajax function. When I view this in my browser debugger I see the following:

http://www.example.com/apps/lhr/iframes/fault_detail.php?Room=phproom

what I need to see is the room number in place of "phproom":

http://www.example.com/apps/lhr/iframes/fault_detail.php?Room=0101

If I hard code the room number in the ajax call it works fine so I know I am not passing the data correctly.

Can anyone please help. Thanks in advance for your time.

Upvotes: 0

Views: 635

Answers (1)

Youssef Houmdi
Youssef Houmdi

Reputation: 48

it's normal that you get phproom, in your javascript, you define the variable "Room" in data as string 'phproom'.

You need to remove the quote to allow to your program to recognize phproom as variable not as string.

Good Luck

Upvotes: 2

Related Questions