Jump to content

[SOLVED] how do you pass a variable to a second php page?


kr3m3r

Recommended Posts

I am working on my first multi-page php project, and was wondering how this works.

On the very first page I have:

 

<select name="itemtype">

which is given a value of 1, 2, or 3 and passed to the next page by:

 

$itemtype = $_POST['itemtype'];

where the value of itemtype is used in a switch statement. 

 

I want to use the itemtype variable again, and add it to my database, so the page can remember if the item is a dvd or cd or book. 

 

$itemtype = $_POST['itemtype'];

 

 

Doesn't seem to be working again is there a way to pass this variable?

 

Thanks

-Robb

Link to comment
Share on other sites

Hi..

 

/************** SAVE THIS CODE AS "page1.php"***********************/
<form name="frm1" action="page2.php" method="post">
    <select name="itemtype">
         <option value="1">CD</option >
         <option value="2">DVD</option >
         <option value="3">Book</option >
    </select>
    <input type="submit" name="submit" value="submit">
</from>
/************** SAVE THIS CODE AS "page2.php"***********************/
<?php
   if(isset($_POST['submit']) && $_POST['submit'] == "submit")   
   {
        $itemtype = $_POST['itemtype'];
        echo  $itemtype; // your passed value from page1
    }  
?>

 

Regards,

Vijay

 

 

Link to comment
Share on other sites

Well Vijay, I appreciate the post, but I'm not sure how to apply it to what I'm doing. I thought adding the code may be helpful to the people reading in, so here it is:

 

This is the first page code I'm trying to get the value of itemtype from here

 

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>

<?php
$itemtype = $_POST['itemtype'];

switch($itemtype)
{
case '1';
$itemtype=$itemtype;
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Author</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Publisher</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '2';
$itemtype=$itemtype;
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Main Star</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Director</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '3';
$itemtype=$itemtype;
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Band</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Label</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;
}
?>

</body>
</html>

 

to here:

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>

<?php
//create short variable names
$UPC=$_POST['UPC'];
$title=$_POST['Title'];
$MajorAttribute=$_POST['MajorAttribute'];
$SecondaryAttribute=$_POST['SecondaryAttribute'];
$itemtype = $_POST['itemtype'];

if(!$UPC || !$title || !$MajorAttribute || !$SecondaryAttribute || !$itemtype)
{
echo 'You have not entered all the required details. <br />'
     	     .'Please go back and try again.';
exit;
}

$UPC=addslashes($UPC);
$title=addslashes($title);
$MajorAttribute=addslashes($MajorAttribute);
$SecondaryAttribute=addslashes($SecondaryAttribute);
$itemtype=addslashes($itemtype);

@ $db = mysql_pconnect('school', 'library', 'alpha321');

if(!$db)
{
echo 'Error: Could not connect to database. Please try later.';
exit;
}

mysql_select_db('cisse');

$query = "insert into cisse values 
 ('".$UPC."','".$title."','".$MajorAttribute."','".$SecondaryAttribute."','".$itemtype."')";
$result = mysql_query($query);

if($result)
{
  	echo mysql_affected_rows().' item(s) inserted into database.<br /><br />';
}
?>
<INPUT TYPE = BUTTON Value = "Add Another Book" onClick="window.location.href='http://www.kr3m3r.com/newitem.html'"> 

</body>
</html>

But this second page doens't seem to recognize the itemtype variable.

Link to comment
Share on other sites

I may be wrong here, but it looks like on your first page ( on this last post I mean) you are declaring the $itemtype var to contain the value from $_POST['itemtype'].

 

But if you aren't posting this value TO this page from another page, there will not be a value there.

 

Looks like you might have bigger problems though, I think your form scheme is a little sketchy.

 

Vijay had the right idea I think, considering what info you gave initially. Here's what I'm thinking:

 

Take your form:

echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Author</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Publisher</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';

 

That was your first case of course. On your input type=" blah blah part... the name="whatever" part is what will be posted in the $_POST['whatever'] variable, and whatever variable you are defining from it will be the value of that $_POST['whatever'] var..

 

Here's a test for you:

 

add these echoes to your second page

echo $_POST['UPC']"<br />";
echo $_POST['Title']"<br />";
echo $_POST['MajorAttribute']"<br />";

 

Now go back and fill your form fields out and submit the form.

 

Hope that points you in the right direction

 

 

Link to comment
Share on other sites

when I echo values on the second page, all the _POST values are present.  The only value that isn't present is the itemtype value.  I figure it isn't present because at the second php page, it is no longer a post form value.  So I guess I'm wondering how to pass a value from one page to another without it being part of the form.

 

every time I've used _POST it has been with a variable that is part of a form.

e.g.:

on the pre-insert page MajorAttribute gets its value from:

echo '<tr>';
echo '<td>Author</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';

 

on the insert page MajorAttribute is called from the pre-insert page by:

$MajorAttribute=$_POST['MajorAttribute'];

 

What I'm trying to figure out is how can I use a value declared in the pre-insert page by:

$itemtype = $_POST['itemtype'];

 

On the insert page?

$itemtype = $_POST['$itemtype'];

does not work again.

$itemtype = $itemtype

doesn't work either

nor does any other form of itemtype that I can think of work.

Link to comment
Share on other sites

The reason those $_POST values are present is because you submitted them from the form on the first page. The php interpreter looks for certain tags (in your case the <input tag) and takes their names and values and makes them a part of the $_POST[] array and automatically sends them to the next page. The reason the $_POST['itemtype'] does not work is because you are trying to declare on the first page that $itemtype = $_POST['itemtype'], but on this first page $_POST['itemtype'] does not have a value yet.

 

Its like you are trying to call on a variable that does not exist because it was never transferred from another page to your first page.

 

Anybody feel free to correct me if I'm off base here.

Link to comment
Share on other sites

U can use GET or use session for this situation.

I mostly prefer session to store value and pass it to the nest or other pages.

 

Just try it like this:

on the top of every page start ur session like:

<?php
session_start();
$itemtype=$_POST['itemtype'];
$_SESSION['itemtype']=$itemtype;
rest of ur code.
?>

 

Now on the 2nd page try it like this:

 

<?php
session_start();
$itemtype=$_SESSION['itemtype'];
And the ret of ur code
?>

 

Hope this will help.

Link to comment
Share on other sites

the itemtype is just a tracker I'm using to note if the user is inputing books(1), dvds(2), or cds (3).  Then based on what that says, the itemtype will be used to insert the correct prefaces to the item's characteristics.  i.e. if you have a book, then you'll be prompted for the UPC, the Title, the Author, and the Publisher,  but if you have a dvd you'll be prompted for the UPC, the title, the main star, and the director.  The information for these items is all stored in the same place, but how it is listed to the user will differ depending on the itemtype.  I know it might be easier to use multiple tables maybe, but it also seems like that would be a lot more repetitive coding.

 

Also, will the Session thing need an end or some sort of closure??? It sounds pretty cool.

Thanks again for all the help.

-Robb

Link to comment
Share on other sites

you can simply use hidden fields in your form or you may pass the value through te url (and access using $_GET [though this is not recommended]) or alternatively as stated earlier, you may save this value in the session variable, latter will be ideal especially if you are doing session handling.

Link to comment
Share on other sites

Try like this:

This is the form which vijay write before...

/************** SAVE THIS CODE AS "page1.php"***********************/
<?php
session_start();
?>
<form name="frm1" action="page2.php" method="post">
    <select name="itemtype">
         <option value="1">CD</option >
         <option value="2">DVD</option >
         <option value="3">Book</option >
    </select>
    <input type="submit" name="getvalue" value="submit">
</from>
/************** SAVE THIS CODE AS "page2.php"***********************/
<?php
   if(isset($_POST['getvalue']))   
   {
        $itemtype = $_POST['itemtype'];
        $_SESSION['itemtype']=$itemtype;
    }  
?>

 

Now in the rest of code do it like this:

 

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>

<?php
$itemtype = $_SESSION['itemtype'];

switch($itemtype)
{
case '1';
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Author</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Publisher</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '2';

echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Main Star</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Director</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '3';

echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Band</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Label</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;
}
?>

</body>
</html>

 

Hope this will help...

Remember this code of select item will be on the same page.

 

And i think if u did not use session u can also get the desired value.

 

 

 

Link to comment
Share on other sites

I'm sorry guys, I can't seem to get the sessions thing to work.  Maybe I'm not doing it right, but I tried the variations you guys gave me.  If it might help, here is all the code I've done so far for this site:

the initial html page is this (this is where the itemtype variable is first created):

 

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>
<form action="preinsert_item.php" method="post">

Choose Item Type to Add to Collection:
<select name="itemtype">
<option value = "1">Book</option>
<option value = "2">DVD</option>
<option value = "3">CD</option>
</select>
<br />
<br />
<input type = "submit" value="Submit">
</form>

</body>
</html>

 

I know this is already in this thread, but in case you fast-forwarded to this point here is the pre-insert item (I can call itemtype successfully here, using the $_POST method)

 

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>

<?php

switch($itemtype)
{
case '1';
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Author</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Publisher</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '2';
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Main Star</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Director</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;

case '3';
echo '<form action="insert_item.php" method="post">';
echo '<table boder="0">';
echo '<tr>';
echo '<td>UPC</td>';
echo '<td><input type="text" name="UPC" maxlength="13" size="13"> <br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Title</td>';
echo '<td><input type="text" name="Title" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Band</td>';
echo '<td><input type="text" name="MajorAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Label</td>';
echo '<td><input type="text" name="SecondaryAttribute" maxlength="100" size="25"><br /></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
break;
}
?>

</body>
</html>

 

and here is the last page, where I can no longer figure out a way to access the itemtype variable:

 

<html>
<head>
<title>Multimedia Library - New Item Entry</title>
</head>

<body>
<h1>Multimedia Library - New Item Entry</h1>

<?php
//create short variable names


$UPC=$_POST['UPC'];
$title=$_POST['Title'];
$MajorAttribute=$_POST['MajorAttribute'];
$SecondaryAttribute=$_POST['SecondaryAttribute'];
$itemtype = $_POST['$itemtype'];

if(!$UPC || !$title || !$MajorAttribute || !$SecondaryAttribute || !$itemtype)
{
echo 'You have not entered all the required details. <br />'
     	     .'Please go back and try again.';
exit;
}

$UPC=addslashes($UPC);
$title=addslashes($title);
$MajorAttribute=addslashes($MajorAttribute);
$SecondaryAttribute=addslashes($SecondaryAttribute);
$itemtype=addslashes($itemtype);

@ $db = mysql_pconnect('school', 'library', 'alpha321');

if(!$db)
{
echo 'Error: Could not connect to database. Please try later.';
exit;
}

mysql_select_db('cisse');

$query = "insert into cisse values 
 ('".$UPC."','".$title."','".$MajorAttribute."','".$SecondaryAttribute."','".$itemtype."')";
$result = mysql_query($query);

if($result)
{
  	echo mysql_affected_rows().' item(s) inserted into database.<br /><br />';
}
?>
<INPUT TYPE = BUTTON Value = "Add Another Book" onClick="window.location.href='http://www.kr3m3r.com/newitem.html'"> 

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.