0

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.

3
  • 5
    Remove ' around phproom? Commented Jan 10, 2017 at 9:21
  • Is your JS being loaded as a separate file, or on the same page? I typically strongly discourage mixing PHP echo statements in JS. What you can do is actually to store the $RoomNo variable as a cookie (using $_COOKIE and then accessing it using JS) or as a HTML5 data- attribute of an element in the document, and then reading the value on DOM ready. Commented Jan 10, 2017 at 9:21
  • try to print the phproom in console. Check if value is present or not Commented Jan 10, 2017 at 9:22

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

12 Comments

Hi and thanks for your reply. I removed the quotes but still does not work.
@Nishant Nair Hi, the JS is on the same page. when I print phproom to the console I see nothing.
try to reorder those lines : var phproom = <?php echo $RoomNo;?> $(document).ready(function() {
hi, when you say reorder "$(document).ready(function() {" what do you mean?
hi, the line [var phproom = <?php echo $RoomNo;?>] must be the first, and followed by [$(document).ready(function() {]
|

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.