Passing variables to multiple pages using input type hidden

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Passing variables to multiple pages using input type hidden

Post by PHP_Ste »

Code: Select all

<input type="hidden" name="validuser" value="<?$_SESSION['valid_user']"?>
Is that the correct syntax to use to do this?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

<input type="hidden" name="validuser" value="<? $_SESSION['valid_user']; ?>">
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Thanks.

It isn't going over onto the next page though for some reason. Do I need to pass it into a variable?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to echo it on the form page:

just doing:

Code: Select all

<input type="hidden" name="validuser" value="<? $_SESSION['valid_user']; ?>">
doesn't output anything.

Try this:

Code: Select all

<input type="hidden" name="validuser" value="<? echo $_SESSION['valid_user']; ?>">
or the shorthand:

Code: Select all

<input type="hidden" name="validuser" value="<?= $_SESSION['valid_user']; ?>">
Burr
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Thanks for your help, one last thing then I'm done for the night.

I need to generate edit boxes using loops, the user specifies how many, which also means I have to insert the data into the database using additional loops. The main problem is with naming each edit box, I'm using this syntax but am not sure if it is right or not:

Code: Select all

><input type="text" name=<?"description ' . $n . '"?> style="width: 175px">
I consulted the php manuel but couldn't find anything on naming edit boxes in loops.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

again, you're not echoing anythign so it won't output anything.

also, it's never good practice to have spaces in your form var names:

try this:

Code: Select all

<input type="text" name="<? echo 'description' . $n; ?>" style="width: 175px">
where $n I assume is the number in the loop.
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Thanks alot. Yes it is the number in the loop:

Code: Select all

><input type="text" name="<? echo 'description'.$n; ?>" style="width: 175px">
That's correct?

I'm getting an unexpected t string error that wasn't there before.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that should do it. One thing you might want to do is just add the final value of $n to a hidden form var so you know how many to loop over on the action page for your queries.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

PHP_Ste wrote: I'm getting an unexpected t string error that wasn't there before.
guessing that was an edit as I didn't see it when I just responded.

can you paste some more of your code in here so I can look over it to see if I can help you find that error?
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Think I might have found the problem, do I have to echo when I'm assigning the form vars to normal vars?

For example:

Code: Select all

$member = $_POST[echo 'description'.$n;];
or is it just

Code: Select all

$member = $_POST['description'.$n;];
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

No, the only time you need to echo is if you want the server to send information down to the client.

If you're going to be handling stuff on the server side, you don't need to echo and in fact echoing the way you have it in ex: 1 above, will make it blow up.

for dynamically named form vars like that, I'd use dbl quotes and just include the $n within:

ex:

Code: Select all

$member = $_POST["description$n"];
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHP_Ste wrote:Think I might have found the problem, do I have to echo when I'm assigning the form vars to normal vars?

For example:

Code: Select all

$member = $_POST[echo 'description'.$n;];
or is it just

Code: Select all

$member = $_POST['description'.$n;];
Both are syntactically wrong. The second is almost correct apart from the semi colon in the [ ].

Do this

Code: Select all

$member = $_POST['description'.$n];
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote: mmm....bacon
Off topic but What happened to buttercups? Is this just your "flavor of the week"? LOL :P
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11wtq wrote:
Burrito wrote: Is this just your "flavor of the week"? LOL :P
I was feeling bacon-ish today... Nuthin' like a nice slab of bacon

...I can hear it sizzling now, 40 more mins of work and I'm off for home for some good 'ole bacon.

:D
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Stop talking about bacon I didn't have time to have any breakfast this morning.

I'm still getting the unexpected t string error. :cry:

Code: Select all

<?php 
session_start(); 
include('connect.inc');
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Welcome to DIYMusic.com</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
<?php

$_SESSION['no_of_influences'] = $_POST['influences']; 
$_SESSION['no_of_members'] =  $_POST['member']; 
if(isset($_POST['validuser'])) { ?> 
<table width="96%" height="600" border="0">
  <tr>
    <td width="31%" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>

<div align="center"><b>Enter the details of each member of your band and each band who has influenced you below:</b></div>

<p></p>
<p><p>
<p></p>

<td width="69%" valign="top">"><p>Hello <? echo $_POST['validuser']; ?>, welcome to the backstage area! <a href="logout.php">LOG OUT</a></p>

<form action="<?=$_SERVER['PHP_SELF']?>"  method="post">

<b><i>Members:</i></b>

<?php
$i=1;

echo'<table border="0">';

while ($i <=  $_SESSION['no_of_members'])
	{
**********************************************************
ERROR BELOW, on the ="<? echo 'member'.$i; ?>" line.

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' 

echo'<tr>';
    echo'<td width="37%"><p><strong>Member</strong></td>
     <td width="63%">
     <input type="text" name="<? echo 'member'.$i; ?>" style="width: 175px">
     </strong></td>
     </tr>';
  echo'<tr> 
    <td><p><strong>Role: </strong></td>
    <td><input type="text" name="<? echo 'role'.$i; ?>" style="width: 175px"></td>
  </tr>';
  echo'<tr> 
    <td><strong>Description</strong></td>
    <td><input type="text" name="<? echo 'mdescription'.$i; ?>" style="width: 175px"></td>
  </tr> ';

		$i++;
	}?>
<tr><td><b><i>Influences:</i></b></td></tr>

<?php

$n=1;

while ($n <=  $_SESSION['no_of_influences'])
	{
		?>
		<p><tr><td>
		<strong>Influence</strong></td>
<td><input type="text" name="<? echo 'influence'.$n; ?>" style="width: 175px">
		</td></tr>

		<tr><td>
		<strong>Description</strong></td>
<td><input type="text" name="<? echo 'description'.$n; ?>" style="width: 175px">
		</td></tr>

		<?
		$n++;
	}

$loginname = $_POST['validuser'];
$sql = "SELECT band_ID FROM user where login_name = '.$loginname.'";
$result = mysql_query($sql);
$bandID = mysql_fetch_array($result);

$i=1;

if(isset($_POST['submit'])) {

while ($i <=  $_SESSION['no_of_members']) 
	{

		$member = $_POST['description'.$i];
		$role = $_POST['role'.$i];
		$mdescription = $_POST['mdescription'.$i];

		$sql = "INSERT INTO member (`band_ID`, `member_name`) 
		VALUES (
		'$bandID', '$member'
		)";

		mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());

		$memberID = mysql_insert_ID();

		$sql = "INSERT INTO memberrole (`member_ID`, `role`) 
		VALUES(

		'$memberID', '$role'

		)";

		mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());

		$sql = "SELECT * FROM role WHERE role = '".$role."' && description = '".$mdescription."'";
		$result = mysql_query($sql) or die("Failed <pre>$sql</pre>".mysql_error());

 if (mysql_num_rows($result) <= 0 ) 
 		
                                       {


		$sql = "INSERT INTO role (`role`, `description`) 
		VALUES(

		'$role', '$description'

		)";

		mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());

		$i++;

		}

$n=1;

while ($n <=  $_SESSION['no_of_influences']) 
	
                  { 

		$influence = $_POST['influence'.$n];
		$description= $_POST['description'.$n];

		$sql = "INSERT INTO bandinfluence (`band_ID`, `influence`) 
		VALUES(

		'$bandID', '$influence';

		)";

		mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());

		$sql = $sql = "SELECT * FROM influence WHERE influence = '".$influence."' && description = '".$description."'";
		$result = mysql_query($sql) or die("Failed <pre>$sql</pre>".mysql_error());

 if (mysql_num_rows($result) <= 0 ) 
 		{

		$sql = "INSERT INTO influence (`influence`, `description`) 
		VALUES(

		'$influence', '$description'

		)";

		mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());

		$n++;
		
		}

	       
                                    }

echo'Your Members and Influences are now in the database, all you need to do now is upload your mp3!';

}

}

}

?>

<tr></tr>
<tr><td></td>
<td><input type="submit" name="submit" value="Submit Details" /></td></tr>

</table>
</form>
</table>
</body> 
</html>
Post Reply