javascript not passing variable back to PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
TIA for your help!

I have a  page (code snip below) named "browse" which has a table in it with
rows like this:

2003 1500 Silverado pickup red 51,024 $19,995  Details

each row of the table has a hidden id number which is to be passed on to the
next page when the user clicks on "Details"  or any other item in the row.

In the code snip below is the javascript that processes the submit when
"Details" is clicked as well as the relevant table code.

When the subsequent page, "details.php" is loaded the following two lines
are executed:
$id = $_REQUEST['d'];
$ids = $_REQUEST['ids'];

In php.ini  "register_globals" is off.

The problem is: Both ['d'] and ['ids] appear to be  recognized but, $id
returns the correct value of "8", while $ids is "" instead of "'2615631'"

What I don't understand is why one works and the other doesn't.
Could someone explain the difference? I've tried some of the tips given for
using extract without any success.


<link href="general.css" rel="stylesheet" type="text/css">

<script language="Javascript">
   var marked_row = new Array;
   function getDetails(obj)
   {  theIDField = eval(''+obj);
      theIDField.checked = "true";
      return true;

    --snipped other code not relevant(I think)

<form method=post name=browse action=details.php><input type=hidden name=d
      <td colspan=9><font color=black>Click Column Headings To
  <tr bgcolor=#D93540>
      <td colspan=9><p class=bigheading><font
      <td colspan=9><br></td>
  <tr bgcolor=#eeeeee id=0 onmouseover="highlight(this);"
      <td valign=top><font color=black><input type=checkbox id=id2615631
name=ids[] value="2615631"></font></td>
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
color=black>1500 SILVERADO</font></td>
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
      <td valign=top onClick="getDetails('2615631'); return true;"><font
          <a href=# onClick="getDetails('2615631'); return

  <tr bgcolor=#ffffff id=1 onmouseover="highlight(this);"
      <td valign=top><font color=black><input type=checkbox id=id2615581
name=ids[] value="2615581"></font></td>
      <td valign=top onClick="getDetails('2615581'); return true;"><font
color=black>2003</font></td><td valign=top

      --snipped-repeat entries for multiple lines in table

Re: javascript not passing variable back to PHP

Marty Meyers wrote:

Quoted text here. Click to load it

To debug, start on the receiving page with:

 <? print_r($_POST); ?>

or $_GET is you send via URL.

You will probably see that ids is not sent at all or empty.

Quoted text here. Click to load it

This is bad.
Have a look at document.getElementById('<ID HERE>');
It is the prefered way of getting elements.

Quoted text here. Click to load it

I would prefer true instead of the string "true";
Unsure if this could be the cause of troubles.

Quoted text here. Click to load it

Please be less sloppy, for your own good.
If you use name/value pairs, be sure you add the values as string.
Now you sometimes do, sometimes dont.

This is better:
<form method="post" name="browse" action="details.php">
<input type=hidden name="d" value="8">

So you are sending "d" with value "8".
You can retrieve it in detais.php as:
$received_d = $_POST["d"];

Quoted text here. Click to load it

Well, you send a string to getDetails, the same string in all cases.

But in your getDetails you receive it named as 'obj'.
Why do you confuse yourself so much with bad names?
It is not an Object, it is a string.

What you should do is this (if I understand you right):

You want the value '2615631' send to the receiving script if somebody hits  
the td, ok?

So just add to your form:
<input type="hidden" name="whatever_ids?" value="NA">

replace whatever_ids? with a good name of course.

ANd in your getdetails function:


Erwin Moller

PS: Debugging starts with:
1) in receiving script print the posting with pre and print_r()
2) in JS: use alert() to check your values.

Quoted text here. Click to load it

Site Timeline