0

I created a form and have gone through a similar question on Stackoverflow as well, relating to this area of sending a variable from one page to another.

I need to pass this value $id=$row['id']; to the next page, but if I store it in the session variable (inside the while lopp) then it stored only the last item and not the current value.

<form name="send" method="POST" action="got.php">
 <hidden id="value" name="value"/>
//<input type="text" id="value">
//<input type="hidden" name="myVariable" value="'.  htmlentities("myVariable").'">;
<input type=submit name=submit value="submit"  onclick="<?php session_start(); $_SESSION(this.id) ?>">
</form>

The full code is given below,

echo "<form name=payment action='properties_details.php'>";
echo "Results <br>";
while ($row= mysql_fetch_array($result)) {
echo " <table border=3 cell padding=1> <tr> <td> ";

$id=$row['id'];
$pid=$row['pref'];
//And the remaining values as well      

//<input type="hidden" name="myVariable" value="'.  htmlentities($pid).'">;
//<input type="hidden" name="myVariable" value="'.  $pid.'">;

echo "<p>Property ID &nbsp".$pid;
echo "<br> <p> Name &nbsp";
echo $row["name"];
echo "<br>  Properties &nbsp";
echo $row["catergory"];
// And printing the remaining values

echo "<input type=submit name=btnbuy value=Details> "; 
9
  • 2
    What's this? <input type=submit name=submit value="submit" onclick="<?php session_start(); $_SESSION(this.id) ?>"> You seem to mix JS with PHP. Commented Jul 16, 2012 at 8:08
  • I tried doing it separately as well (javascript between head tags) but it didnt work Commented Jul 16, 2012 at 8:11
  • You should do it separately. It won't work because you were handling it incorrectly. Commented Jul 16, 2012 at 8:13
  • but in javascript you cannot make SESSION variables, so then it would give the same result as now (last value in the loop) Commented Jul 16, 2012 at 8:14
  • PHP session variables are handled inside the PHP code, not in HTML or JS scripts. Commented Jul 16, 2012 at 8:17

2 Answers 2

2
  • You were not handling PHP sessions correctly.
  • Your onclick event should fire a JS code, not PHP code.
  • PHP is a back-end scripting language, and JS is a front-end scripting language. They don't mix.

Suggestion: Before you proceed with coding, read more about html, forms, passing form variables to php, php sessions etc.

Resources/Tutorials:

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

3 Comments

I tried the javascript technique however the variable is empty in the next page
@Yoosuf show me the exact technique you made. Let us discuss this in chat.
1

you can pass your $id=$row['id'] on another page using query string easily.

on your button pass query string like this.

href="page_name.php?xyz=value you want to pass on second page"

1.here page_name.php is the page name on which you want to pass the value of $row['id'];

2.xyz is the query string name with the help of this we can get id on page_name.php

on page_name.php get query string as:

$id=$_GET["xyz"];

here xyz is a query string variable passed on button click

After that you can easily use this $row ['id'] on second page by using $id variable.

like this

echo $id;

1 Comment

on submit button pass query string as <button type="button" href="secon_page.php?xyz<?php echo $row['id']; ?>">

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.