Passing variables between pages

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

Post Reply
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Passing variables between pages

Post by nmphpwebdev »

I should be able to pass variables between to HTML forms if I use the same variables on both of the pages ....correct??

My pages are:

Page 1

Code: Select all

<?php

include_once('dbconnect.php');

//        Set up query
//        NOTE:  Temporarily set the limit to 5 during testing.

$query = 'select id, event, DATE_FORMAT( startdate, "%M %d, %Y" ) as sd,
         DATE_FORMAT( enddate, "%M %d, %Y" ) as ed, sponsor, location,
         contact, comments from events where status = 0 order by startdate ';

$result = mysql_query( $query ) or die(mysql_error());

//        These are the messages that get sent when a calendar item is approved or disapproved.
$message = '<p>This is a test message for approval</p>';
$message2 = '<p>This is a test for disapproval</p>';

echo '
        <h1>Calendar Administration</h1>';

echo '
        <table width="100%" border="1">
        <tr>
                <td><strong>Event</strong></td>
                <td><strong>Start Date</strong></td>
                <td><strong>End Date</strong></td>
                <td><strong>Sponsor</strong></td>
                <td><strong>Location</strong></td>
                <td><strong>Contact Person</strong></td>
                <td><strong>Comments</strong></td>
                <td colspan="3"><strong>Status</strong></td>
        </tr>
        <tr>';

while ( $value = mysql_fetch_assoc( $result ))
        {

                //        All but the last column simply display data.

                echo "<td>$value[event]</td>";
                echo "<td>$value[sd]</td>";
                echo "<td>$value[ed]</td>";
                echo "<td>$value[sponsor]</td>";
                echo "<td>$value[location]</td>";
                echo "<td>$value[contact]</td>";
                echo "<td>$value[comments]</td>";
                echo "<td>$value[status]</td>";

                //        Final column has the Approve / Disapprove stuff
                if ( $_REQUEST['action'] == 'Approve' and (int)($_REQUEST['calendar_ID']) == $value['id'] )
                {
                        //        They hit the approve button for this row
                        //        Send the approval email and notify user.

                        mail($_REQUEST['email'], $value['event'], $message,
                        "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
                        "Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
                        "X-Mailer: PHP/" . phpversion());
                        print('<td>Approval sent.</td>');
                        $sql = 'UPDATE events SET status = 1 WHERE ID = $_REQUEST[calendar_id] or die(mysql_error() )';
                }
                elseif ( $_REQUEST['action'] == 'Disapprove' and (int)($_REQUEST['calendar_ID']) == $value['id'] )
                {
                        //        They hit the disapprove button for this row
                        //        Send the disapproval email and notify user.

                        mail($_REQUEST['email'], $value['event'], $message2,
                        "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
                        "Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
                        "X-Mailer: PHP/" . phpversion());
                        print('<td>Disapproval sent.</td>');
                        $sql2 = 'UPDATE events SET status = ''2'' WHERE ID = $_REQUEST[calendar_id] or die(mysql_error() )';
                }
                //They hit the edit button for this row
                //Redirects to editevents.php for admin changes
                elseif ( $_REQUEST['action'] == 'Edit Event' and (int)($_REQUEST['calendar_ID']) == $value['id'] )
                {
                 header("Location: editevents.php");
                 }
                else
                {
                          //        Just normal display for this row
                        //        Display the option to approve or disapprove.

                        echo '
                        <td>
                                <form method="post" action="' . $_SERVER['PHP_SELF'] . '">
                                        <input type="hidden" name="calendar_ID" value="' . $value['id'] . '" />
                                        <input type="hidden" name="email" value="stevec@nmsu.edu" />
                                        <input type="submit" name="action" value="Approve" />
                                        <input type="submit" name="action" value="Disapprove" />
                                        <input type="submit" name="action" value="Edit Event" />
                                </form>
                        </td>';
                }

                echo '
        </tr>';
         }
echo '
</table>';
echo '<a href="../index.php">Home</a>&nbsp;§&nbsp;<a href="index.php">Calendar Administration</a>';

?>
Page 2

Code: Select all

<?php

include_once( 'dbconnect.php' );


echo'<h1>Edit Event</h1>
     <form action="editevents.php" method="post">
     <table>
     <th align=center colspan=2>Enter your event information in the form below:</th>';

$result = mysql_query( "select *, DATE_FORMAT( startdate, '%M %d, %Y' ) as sd,
         DATE_FORMAT( enddate, '%M %d, %Y' ) as ed from events where '$calendar_ID' = '$_GET[id]' " );

echo"<tr><td><b>Title:</b></td>";
echo"<td><input type=text name=startdate size=30 maxlength=30 value= '$value[title]' /></td></tr>";
echo"<tr><td><b>Start Date:</b></td>";
echo"<td><input type=text name=startdate size=30 maxlength=30 value= '$value[sd]' /></td></tr>";
echo"<tr><td><b>End Date:</b></td>";
echo"<td><input type=text name=enddate size=30 maxlength=30 value= '$value[ed]' /></td></tr>";
echo"<tr><td><b>Event:</b></td>";
echo"<td><input type=text name=event size=30 maxlength=30 value= '$value[event]' /></td></tr>";
echo"<tr><td><b>Location:</b></td>";
echo"<td><input type=text name=location size=30 maxlength=30 value= '$value[location]' /></td></tr>";
echo"<tr><td><b>Sponsor:</b></td>";
echo"<td><input type=text name=location size=30 maxlength=30 value= '$value[sponsor]' /></td></tr>";
echo"<tr><td><b>Contact Person</b></td>";
echo"<td><input type=text name=contact size=30 maxlength=30 value= '$value[contact]' /></td></tr>";
echo"<tr><td><b>Your Phone:</b></td>";
echo"<td><input type=text name=phone size=30 maxlength=30 value= '$value[phone]' /></td></tr>";
echo"<tr><td><b>Email:</b></td>";
echo"<td><input type=text name=email size=30 maxlength=30 value= '$value[email]' /></td></tr>";
echo"<tr><td><b>Submitted By:</b></td>";
echo"<td><input type=text name=submittername size=30 maxlength=30 value= '$value[submittername]' /></td></tr>";
echo"<tr><td><b>Phone:</b></td>";
echo"<td><input type=text name=submitterphone size=30 maxlength=30 value= '$value[submitterphone]' /></td></tr>";
echo"<tr><td><b>Email</b></td>";
echo"<td><input type=text name=submitteremail size=30 maxlength=30 value= '$value[submitteremail]' /></td></tr>";
echo"<tr><td><b>Comments</b></td>";
echo"<td><textarea cols=22 rows=5 name=comments value= '$value[comments]' >$value[comments]</textarea></td></tr>";

echo'<tr>
     <td align="center" colspan="2">

     <input type="submit" name="submit" value="Update Record" />&nbsp;&nbsp;
     <input type="reset" name="action" value="Clear Form" /></td>';
echo'</tr>
     </table>
     </form>';

if( $_POST['submit'] )
{
$sql = mysql_query("update events set
title = '$value[title]',
startdate = '$value[sd]',
enddate = '$value[ed]',
event = '$value[event]',
location = '$value[location]',
sponsor = '$value[sponsor]',
contact = '$value[contact]',
phone = '$value[phone]',
email = '$value[email]',
submittername = '$value[submittername]',
submitterphone = '$value[submitterphone]',
submitteremail = '$value[submitteremail]',
comments = '$value[comments]'
where id = '$value[id]' ");
printf("Record updated successfully: %d\n", mysql_affected_rows() );
} else {
echo '<p><font color = "red" >Your record could not be updated. ' . mysql_error() . '<br />
We appologize for any inconvenience.<br /> Please try again</font></p>';
}
//End of Form
//Update record with new information
?>
When I am looking at the second page, I have either undefined variables or undefined indexes?

What am I overlooking??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you never created the $value variable by fetching the data. Btw, line 15's input field needs its name changed.

It's also suggested to not look for the submit button as a passed value, since a form can be easily submitted without such a value, while being entirely valid.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

post or get

Post by phpScott »

On page 2 on lines 11/12 you have

Code: Select all

<?php
$result = mysql_query( "select *, DATE_FORMAT( startdate, '%M %d, %Y' ) as sd,
         DATE_FORMAT( enddate, '%M %d, %Y' ) as ed from events where '$calendar_ID' = '$_GET[id]' " );
?>
then on line 50 you have

Code: Select all

<?php
if( $_POST['submit'] )
?>
are you using post or get? as that might be causing some confusion as well.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

Thanks for noticing the incorrect name. I thought I got them all.

I thought that I would have to have the submit button from the form to execute the query.

I added:

Code: Select all

while ( $value = mysql_fetch_assoc( $result ) )
        { (form from above lines 15 - 40 )
         }
I changed the query to:

Code: Select all

$result = mysql_query( "select *, DATE_FORMAT( startdate, '%M %d, %Y' ) as sd,
         DATE_FORMAT( enddate, '%M %d, %Y' ) as ed from events where id = '$calendar_ID' " );
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$calendar_ID isn't specified in your second script.

Additionally, you'll need to quote all HTML attribute values, since you are using XHTML..
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

phpScott - I caught that also, I am using post to submit the changes back to the database.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

feyd - caught that one also.

if i take out the where claus of the sql statement, I get results. BUT, I am getting the same record, no matter which line on the first script i am trying to edit.

I am having trouble passing the the calendar_ID to match against the database id to get the correct record to edit.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pass it via the url.. just make sure you store it has a hidden field, or again in the form's action url.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

How can i amend the form action ( <form method="post" action="' . $_SERVER['PHP_SELF'] . '"> ) to pass the calendar_ID in the url??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

like creating any other "dynamic" link...

Code: Select all

echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?id=' . urlencode($calendar_ID) . '">';
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Note that using code such as:

Code: Select all

if( $_POST['submit'] )
will cause notices if your error reporting is set at its highest level (as it should be for development) and the submit button is not pressed but enter is used to submit the form. A better test would be:

Code: Select all

if (isset($_POST['submit']))
but to avoid browser issues with form submission, even better would be to set a hidden field in the form and test for that value instead. So if you had:

Code: Select all

&lt;input type="hidden" name="action" value="post" /&gt;
then to test whether the form had been submitted you could do:

Code: Select all

if (isset($_POST['action']))
Mac
Post Reply