Jump to content

Returning a value to a php variable


mike2325

Recommended Posts

Hi,

 

On my webpage I am currently able to bring up results from a sql database in a table. The results shown in a table correspond to a date entered by the user. Each row of the table has an ID and there is also a check box on each row of the table enabling the user to select that row in a booking process. Currently, I can get the ID to alert when the check box is clicked with this code:

<td class="cell" align="center" height="19"><input type="radio" name="rad1" onclick="alert('.$row["id"].');" value="'.$row["id"].'"></td>

. However, I need to get this row ID to be assigned to a variable so that it can be passed onto a different webpage using php sessions. I have tried returning the variable, assigning it, everything but I can't get my head round how to do this! It seems that if I can alert it correctly, it must be easy to assign the alerted value to a variable.

 

Any suggestions would be really helpful!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/196230-returning-a-value-to-a-php-variable/
Share on other sites

Remember, every page that you want to use sessions on requires that you have session_start at the beginning of your code.  So, something like:

 

session_start();

// code code code

$_SESSION['rowId'] = $row['id'];

 

In the other file:

 

session_start();

$rowId = $_SESSION['rowId'];

 

That said, I think your problem stems from some confusion over how PHP and JavaScript interact with each other.  Quite simply, unless you're using AJAX, they don't.

 

PHP can't automatically tell which checkbox is selected.  Your PHP script is finished running once your page is displayed on the screen.  JavaScript runs in the browser, which is why it can react to things like click events.

 

You have two ways of sending the checkbox data to the other page - AJAX, which is probably more complicated than you want to get, or making your table a form and posting the values to the other page when the form is submitted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.