Set php variable value in a hidden field ... help required

Hi I am new to php…,

I have some code (see below) that fetches a row of data, one of the values is ‘id’ , i want to pass value ‘id’ to the next form used, (see below) and am looking to pass it as a hidden value…

See the two options i have be trying… but am unable to get to work…

Work be grateful of any advise…

Thanks


<form action="insert_story.php" method="post" onsubmit="return validate()">
<input type="hidden" name="page_number" value="1" />

<?PHP

    $user_name = "ererewr";
    $password = "ewrerer";
    $database = "ewrewrer";
    $server = "ewrwerer";

$con = mysql_connect($server ,$user_name ,$password);
$db_found = mysql_select_db($database);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$rchar=chr(rand(65,67));
$mysql = mysql_query("SELECT * FROM storydetails WHERE storyno = '1' AND partno = '$rchar' AND used <10");

while($row = mysql_fetch_array($mysql))
  {
  echo $row['text']. "<P/>" . "Story Number:" . $row['storyno']." Part: " . $row['partno']." Used: " . $row['used']. " ID : ". $row['id'];
  echo "<br />";
  }
  
echo "<p/>";
echo "<p/>";

mysql_close($con);
?> 

<br />
<input type="hidden" name="story1_id" value=$row['id'] />                               [B]Attempt 1[/B]
<input type="hidden" name="story1_id" value="<?php echo $row['id']; ?>" />       [B]Attempt 2[/B]
<input type="submit" />
</form>

I want to pass ‘id’ to the form below… to be used in the UPDATE function.


<?PHP
ob_start();
    $user_name = "erewr";
    $password = "erewrew";
    $database = "ewrerer";
    $server = "ewrer";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
	
if ($db_found) {

      
$mysql="INSERT INTO responses (response) VALUES ('$_POST[answer1]')";
$result = mysql_query($mysql);

//
$theid = $_POST['story1_id'];
echo $_POST['story1_id'];

$mysql1="UPDATE storydetails SET used='$_POST[used]' WHERE id='$_POST[id]'"; 
$result = mysql_query($mysql1);
//

mysql_close($db_handle);

echo "id=  $theid <br />";
echo "num= $numused <br />";
echo  $_POST['story1_id'];
echo  $_POST['story1_used'];

print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

// Go to next story ....
if (isset($_POST['page_number'])){
$page = $_POST['page_number'];
}

$nextpage = $page+1;
if ($nextpage == 4) {
	header("location: thanks.htm");
} else {
header("location: story$nextpage.php");
}
ob_flush(); 
?>

You should use session variables for this sort of thing.

<input type=“hidden” name=“story1_id” value=$row[‘id’] /> Attempt 1
<input type=“hidden” name=“story1_id” value=“<?php echo $row[‘id’]; ?>” /> Attempt 2

Can’t have 2 input with same name, or you will get an array on next page. Try Attempt 2 only.

U already got the $_POST on insert_story.php.

However if you want to add the id to header(“location: story$nextpage.php”);

Then do header(“location: story$nextpage.php?id=”.$your_id_var);

Then on next page, u do $_GET

Welcome to the SP forums.

To see if your hidden form field has the right value, you can check out the html code in your browser. Attempt 2 should work.

I see you already do an echo of $_POST[‘story1_id’]. What does that show?

And may I ask why you are using ob_start() and ob_flush() in the second script?

Hi and thanks for the replies.

@guido2004

if i use : <input type=“hidden” name=“story1_id” value=“<?php echo $row[‘id’]; ?>” />

the result of the echo in the second page = ‘’ (nothing)

re : buffering… i was getting an error … headers passed or similar… have just removed… and now no error.

on second page,
after: <?php
type :
echo “<pre>”;
print_r($_POST);
echo “</pre>”;
exit;

then copy the result and paste here.

Here are the results : page_number & answer1 are as expected.

Array
(
[page_number] => 1
[answer1] => this is the 3rd Answer (story 1 C3)
[story1_id] =>
)

That mean :
<input type=“hidden” name=“story1_id” value=“<?php echo $row[‘id’]; ?>” />

have value = “”

Change type=“hidden” to type=“text”
and you will see it get some value on text field or not.

If it get blank, that mean your query return 0 results :smiley:

ok changed to text , result =

Array
(
[page_number] => 1
[answer1] => this is the 3rd Answer (story 1 C3)
[story1_id] =>
)

But it has a value as i echo the ID with this line of code : echo $row[‘text’]. “<P/>” . “Story Number:” . $row[‘storyno’]." Part: " . $row[‘partno’]." Used: " . $row[‘used’]. " ID : ". $row[‘id’];

you can see the actual page here : http://www.fay-holloway.co.uk/story1.php

What is it ?


this is the 1st Answer (story 1 B1) + 1 + B + 
this is the 2nd Answer (story 1 B2) + 1 + B + 
this is the 3rd Answer (story 1 B3) + 1 + B + 
this is the 4th Answer (story 1 B4) + 1 + B + 
this is the 5th Answer (story 1 B5) + 1 + B + 

B + what ? your $row array change after u run 2nd query.

I suggest after this line:


 echo $row['text']. "<P/>" . "Story Number:" . $row['storyno']." Part: " . $row['partno']." Used: " . $row['used']. " ID : ". $row['id'];

You have 1 more line:


$my_id = $row['id'];

Then at attempt 2
<input type=“text” name=“story1_id” value=“<?=$my_id?>” />

Check and tell me the result on text field, if it work.

i put the : $my_id = $row[‘id’];

if the while loop … and bingo !!! its working

Thank you Sir … :slight_smile: