header() not redirecting for some reason...
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Here is a generic example of a form...
Now all of these form fields will be sent in the $_POST array to process_page.php. The members of the $_POST array will be $_POST['field1'], $_POST['hidden_field'] and $_POST['send_form']. On process_page.php, these values can be assigned to variables for use throughout that page. So, for example, to use the text field entry and the hidden field, you could something like this...
I know the example is weak, but it is the essential logic that is used to build these types of scripts. Of course there is more, but this is the general logic you want to follow.
Code: Select all
<form method="post" action="process_page.php" id="this_form">
<input type="text" name="field1" value="some default value to show in the form" />
<input type="hidden" name="hidden_field" value="hidden_value" />
<input type="submit" name="send_form" value="Send this form" />
</form>Code: Select all
<?php
// Really quick check for form vars being sent
if (isset($_POST['field1']))
{
$field1 = $_POST['field1'];
$hidden_field =$_POST['hidden_field'];
// now you can use these vars for cleansing, filtering and validation... or whatever
}
else
{
// Oops, the form value was not set, so we should probably show the form here
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
everah i really appreciate you taking the time and helping me with my script. this is becoming a nightmare fast. now, i'm starting to think that i NEED redirect.php... i've made some changes to the code. it's so weird. the input fields are being pre-populated. and, when i edit them, i have to click save TWICE for it to make the changes in the database... for instance, i'll load the editCouple.php page... replace the pre-populated input field that has 'Sherry' in it, and type 'Clarise'. then i click save.. it reloads the page, but still has 'Sherry' in the input field. then, all i do is click save again (without typing anything)... and THEN it will save it. why is it taking two times clicking to work?
and here's another really weird thing... so i load the editCouple.php page. it's got all the info in the right places. then, i won't type or change anything, then i click save... it reloads, and the entire page is empty. it has no information being populated in the input fields... then i click save again... and magically... it all appears again. i can do this until i'm blue in the face.... what the heck is going on?
here is my code now.
and here's another really weird thing... so i load the editCouple.php page. it's got all the info in the right places. then, i won't type or change anything, then i click save... it reloads, and the entire page is empty. it has no information being populated in the input fields... then i click save again... and magically... it all appears again. i can do this until i'm blue in the face.... what the heck is going on?
here is my code now.
Code: Select all
<?php
$brideFname = $_POST['brideFname'];
$brideLname = $_POST['brideLname'];
$groomFname = $_POST['groomFname'];
$groomLname = $_POST['groomLname'];
$_POST['event_month2'];
$_POST['event_day2'];
$_POST['event_year2'];
$event_month = $_POST['event_month'];
$event_day = $_POST['event_day'];
$event_year = $_POST['event_year'];
#$_POST['editID'];
#$_POST['ship_add'];
#$_POST['ship_city'];
#$_POST['ship_state'];
#$_POST['ship_zip'];
$uID = $row['uID'];
if($_POST['event_month2'] != NULL){
$event_month = $_POST['event_month2'];
}if($_POST['event_day2'] != NULL){
$event_day = $_POST['event_day2'];
}if($_POST['event_year2'] != NULL){
$event_year = $_POST['event_year2'];
}
@ $db = mysql_connect("localhost", "apache", "nopass");
mysql_select_db("registry_DB", $db);
if(!$db){
echo "Error: Could not connect to the database. Please try again later.";
exit;
}
$sql = "UPDATE my_search_table ".
"SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
"groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
"event_year = '$event_year' ".
"WHERE uID = '$uID'";
mysql_query($sql) or die(mysql_error());
mysql_close($db);
echo "<TABLE><TR><TD>";
echo "<form action=editCouple.php?editID=". $row['uID'] ." method=post>";
echo "<B>Full name:</B> ". $row['brideFname'] ." ". $row['brideLname'] ."<br />";
echo "<B>Bride's First Name:</B> <input type=text name=brideFname value=". $row['brideFname'] ."><br />";
echo "<B>Bride's Last Name:</B> <input type=text name=brideLname value=". $row['brideLname'] ."><br /><br />";
echo "<B>Full name:</B> ". $row['groomFname'] ." ". $row['groomLname'] ."<br />";
echo "<B>Groom's First Name:</B> <input type=text name=groomFname value=". $row['groomFname'] ."><br />";
echo "<B>Groom's Last Name:</B> <input type=text name=groomLname value=". $row['groomLname'] ."><br /><br />";
echo "<B>Event date:</B> ". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br /><br />";
?>
Change the date using the drop down menus below:<BR>
<SELECT NAME="event_month2">
<OPTION VALUE="">select a month
<OPTION VALUE="01">January
<OPTION VALUE="02">February
<OPTION VALUE="03">March
<OPTION VALUE="04">April
<OPTION VALUE="05">May
<OPTION VALUE="06">June
<OPTION VALUE="07">July
<OPTION VALUE="08">August
<OPTION VALUE="09">September
<OPTION VALUE="10">October
<OPTION VALUE="11">November
<OPTION VALUE="12">December
</SELECT>
<SELECT NAME="event_day2">
<OPTION VALUE="">select a day
<OPTION VALUE="01">01
<OPTION VALUE="02">02
<OPTION VALUE="03">03
<OPTION VALUE="04">04
<OPTION VALUE="05">05
<OPTION VALUE="06">06
<OPTION VALUE="07">07
<OPTION VALUE="08">08
<OPTION VALUE="09">09
<OPTION VALUE="10">10
<OPTION VALUE="11">11
<OPTION VALUE="12">12
<OPTION VALUE="13">13
<OPTION VALUE="14">14
<OPTION VALUE="15">15
<OPTION VALUE="16">16
<OPTION VALUE="17">17
<OPTION VALUE="18">18
<OPTION VALUE="19">19
<OPTION VALUE="20">20
<OPTION VALUE="21">21
<OPTION VALUE="22">22
<OPTION VALUE="23">23
<OPTION VALUE="24">24
<OPTION VALUE="25">25
<OPTION VALUE="26">26
<OPTION VALUE="27">27
<OPTION VALUE="28">28
<OPTION VALUE="29">29
<OPTION VALUE="30">30
<OPTION VALUE="31">31
</SELECT>
<SELECT NAME="event_year2">
<OPTION VALUE="">select a year
<OPTION VALUE="2002">2002
<OPTION VALUE="2003">2003
<OPTION VALUE="2004">2004
<OPTION VALUE="2005">2005
<OPTION VALUE="2006">2006
<OPTION VALUE="2007">2007
<OPTION VALUE="2008">2008
<OPTION VALUE="2009">2009
<OPTION VALUE="2010">2010
</SELECT></TD></TR>
<TR><TD>
<?php
/*********************DEBUG************************
echo $row['ship_add'] ."<br />";
echo "<B>Address:</B> <input type=text name=ship_add value=". $row['ship_add'] ."></br />";
echo "<B>City:</B> <input type=text name=ship_city value=". $row['ship_city'] ."></br />";
echo "<B>State:</B> <input type=text name=ship_state value=". $row['ship_state'] ."></br />";
echo "<B>Zip Code:</B> <input type=text name=ship_zip value=". $row['ship_zip'] ."></br /><br />";
**************************************************/
?>
<TABLE width=400><TR>
<BR>
<?php
$_POST['brideFname'];
$_POST['brideLname'];
$_POST['groomFname'];
$_POST['groomLname'];
$_POST['ship_add'];
$_POST['ship_city'];
$_POST['ship_state'];
$_POST['ship_zip'];
$_POST['event_month2'];
$_POST['event_day2'];
$_POST['event_year2'];
$_POST['event_month'];
$_POST['event_day'];
$_POST['event_year'];
$_POST['editID'];
?>
<TD align=center valign=left><input type=submit value="Save"></form></TD>
<TD align=center valign=right>
<form method=post action=updateRegistry.php?regID=<?php echo $uID; ?>><input type=submit value="Continue to Registry >>">
</form>
</TD></TR></TABLE>
</TD></TR></TABLE>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
It doesn't take two saves, it just appears that way. The reason is the $row var. It is being set from a query result (it appears that way anyway). You are populating your form with data from the query, so eventhough you are updating correctly, the updated information is not getting to the form because the query result was pulled and set before the update. So when the form shows itself (after posting) what you have is a $row array that has information in it from the previous data set and a database entry for the new data, though you are showing the information from the old dataset.
The appearance of a necessary second save is because the $row array gets set with the new information on the second pass, eventhough it was there in the step before.
What I would do to prove what I am talking about is after the insert/update of the information, run another select query and use that information in your form. You'll see what I am talking about...
The appearance of a necessary second save is because the $row array gets set with the new information on the second pass, eventhough it was there in the step before.
What I would do to prove what I am talking about is after the insert/update of the information, run another select query and use that information in your form. You'll see what I am talking about...
no need to verify my friend =). i'm positive this is the case. i figured it was something of that nature, logically it makes sense. so what i did was place the $row in front of all the variables in the beginning, like so...
now, it works the first time i press save... but there's something wrong with the event_dates... they're not posting. and they're not being replaced. it's weird... it may have something to do with my IF satements... i don't know. i haven't changed any of the code responsible for that, tho. it's still the same. trying to make some editions now..
Code: Select all
$row['brideFname'] = $_POST['brideFname'];
$row['brideLname'] = $_POST['brideLname'];
$row['groomFname'] = $_POST['groomFname'];
$row['groomLname'] = $_POST['groomLname'];
$_POST['event_month2'];
$_POST['event_day2'];
$_POST['event_year2'];
$row['event_month'] = $_POST['event_month'];
$row['event_day'] = $_POST['event_day'];
$row['event_year'] = $_POST['event_year'];
#$_POST['editID'];
#$_POST['ship_add'];
#$_POST['ship_city'];
#$_POST['ship_state'];
#$_POST['ship_zip'];it's so weird... when i click a link to go to editCouple.php... the input fields are completely blank... all of them. and when i click 'back' on my browser, all the information is there, except somehow, the event_date fields get deleted automatically... then i click editCouple.php again.... and it's got nothing, still... so i'll type in some information... drop the dropdown menu and click a date field. then click save... then all the info is stored and when it reloads it shows.. except the event_date.... then without typing anything.... i'll click save again.... and NOW everything shows, including the event_date.... but then i'll click back to the home screen, and all the new information is stored, except the date.... even tho it showed me, after two clicks, that is was there..... NIGHTMARE on ZEND street....
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
dude, you are the MAN. but i think this is causing so much of a headache that i'm going to start all over with this thing... i'm building an entirely new editCouple.php... and building a redirect.php to handle all the sql queries... and it will redirect to editCouple.php, which will display the information as it is in the database... since it's mostly copy and paste, i'll put both pages on here in a few minutes... thanks SO much, man, you don't even know how awesome you are.
so here's the first page.... editCouple.php....
so far everything works beautifully. everything is being populated correctly... i'm writing the redirect.php right now. i'll post it in another few minutes.
Code: Select all
<HTML>
<HEAD><TITLE>Edit Couple</TITLE></HEAD>
<BODY>
<CENTER>
<?php $location = "<B>Edit Couple</B>"; ?>
<TABLE BORDER="1" WIDTH="850" CELLPADDING="20">
<TR>
<TD WIDTH="%50" ALIGN="center"><H1>The Very Thing Gifts</H1>
<?php echo $location; ?>
<form method="POST" action="admin1.php?action=view_all">
<input type="SUBMIT" value="View All">
</form>
</TD>
<TD><?php include "admin_search.inc" ?></TD>
</TR>
</TABLE><BR>
<HR WIDTH="300"><BR>
<TABLE WIDTH="750"><TR><TD>
<?php
$uID = $_POST['editID'];
$status = $_POST['status']; //sent from redirect.php
@ $db = mysql_connect("localhost", "apache", "nopass");
if(!$db){
echo "Error: Could not connect to the database. Please try again later.";
exit;
}
mysql_select_db("registry_DB", $db);
//retrieve info from the database
$sql = mysql_query("SELECT * FROM my_search_table WHERE uID LIKE '%". $editID ."%'") or die(mysql_error());
$row = mysql_fetch_array($sql);
//pre-populate input fields with values from the database.
echo "<form action=redirect.php?ID=". $row['uID'] ." method=post>";
echo "<input type=hidden name=ID value=". $uID .">";
echo "<B>Bride First:</B> <input type=text name=brideFname value=". $row['brideFname'] ."><br />";
echo "<B>Bride Last:</B> <input type=text name=brideLname value=". $row['brideLname'] ."><br />";
echo "<B>Groom First:</B> <input type=text name=groomFname value=". $row['groomFname'] ."><br />";
echo "<B>Groom Last:</B> <input type=text name=groomLname value=". $row['groomLname'] ."><br /><br />";
echo "<B>Event Date:</B> ". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br />";
echo "<B>Change event date:</B> ";
?>
<SELECT NAME="event_month2">
<OPTION VALUE="">select a month
<OPTION VALUE="01">January
<OPTION VALUE="02">February
<OPTION VALUE="03">March
<OPTION VALUE="04">April
<OPTION VALUE="05">May
<OPTION VALUE="06">June
<OPTION VALUE="07">July
<OPTION VALUE="08">August
<OPTION VALUE="09">September
<OPTION VALUE="10">October
<OPTION VALUE="11">November
<OPTION VALUE="12">December
</SELECT>
<SELECT NAME="event_day2">
<OPTION VALUE="">select a day
<OPTION VALUE="01">01
<OPTION VALUE="02">02
<OPTION VALUE="03">03
<OPTION VALUE="04">04
<OPTION VALUE="05">05
<OPTION VALUE="06">06
<OPTION VALUE="07">07
<OPTION VALUE="08">08
<OPTION VALUE="09">09
<OPTION VALUE="10">10
<OPTION VALUE="11">11
<OPTION VALUE="12">12
<OPTION VALUE="13">13
<OPTION VALUE="14">14
<OPTION VALUE="15">15
<OPTION VALUE="16">16
<OPTION VALUE="17">17
<OPTION VALUE="18">18
<OPTION VALUE="19">19
<OPTION VALUE="20">20
<OPTION VALUE="21">21
<OPTION VALUE="22">22
<OPTION VALUE="23">23
<OPTION VALUE="24">24
<OPTION VALUE="25">25
<OPTION VALUE="26">26
<OPTION VALUE="27">27
<OPTION VALUE="28">28
<OPTION VALUE="29">29
<OPTION VALUE="30">30
<OPTION VALUE="31">31
</SELECT>
<SELECT NAME="event_year2">
<OPTION VALUE="">select a year
<OPTION VALUE="2002">2002
<OPTION VALUE="2003">2003
<OPTION VALUE="2004">2004
<OPTION VALUE="2005">2005
<OPTION VALUE="2006">2006
<OPTION VALUE="2007">2007
<OPTION VALUE="2008">2008
<OPTION VALUE="2009">2009
<OPTION VALUE="2010">2010
</SELECT></TD></TR>
<TR><TD>
<?php
echo "<B>Address:<B> ". $row['ship_add'] .", ". $row['ship_city'] .", ". $row['ship_state'] .", ". $row['ship_zip'] ."<br /><br />";
mysql_close($db);
echo "<B>Change address information</B><br />";
echo "Street: <input type=text name=ship_add><br />";
echo "City: <input type=text name=ship_city><br />";
echo "State: <input type=text name=ship_state><br />";
echo "Zip Code: <input type=text name=ship_zip><br />";
$_POST['ID'];
$_POST['brideFname'];
$_POST['brideLname'];
$_POST['groomFname'];
$_POST['groomLname'];
$_POST['ship_add'];
$_POST['ship_city'];
$_POST['ship_state'];
$_POST['ship_zip'];
$_POST['event_month'];
$_POST['event_day'];
$_POST['event_year'];
$_POST['event_month2'];
$_POST['event_day2'];
$_POST['event_year2'];
?>
<input type=submit value=Save></form>
</TD></TR></TABLE>
Last edited by boo_lolly on Thu Nov 30, 2006 4:42 pm, edited 1 time in total.
just a tip:
better way of populating a select when it's just a range of numbers...
Then it's really easy to reselect it if you need to...
better way of populating a select when it's just a range of numbers...
Code: Select all
<?php
$days = range(1, 31);
echo "<select name=\"days\">\n";
echo " <option value=\"\"> - Select One - </option>";
foreach($days as $day)
{
echo " <option value=\"" . $day . "\">" . $day . "</option>\n";
}
echo "</select>\n";
?>Code: Select all
<?php
$days = range(1, 31);
$selected_day = isset($_POST['day']) ? (integer) $_POST['day'] : null;
echo "<select name=\"days\">\n";
echo " <option value=\"\"> - Select One - </option>";
foreach($days as $day)
{
$selected = ($selected_day == $day) ? 'selected="selected"' : '';
echo " <option value=\"" . $day . "\" " . $selected . ">" . $day . "</option>\n";
}
echo "</select>\n";
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You're going to get a parse error after this line:
You have a quote that doesn't close. Get rid of the opening double quote.
Code: Select all
$status = "$_POST['status'];thank you, ninja, for that extremely helpful tip on the drop down menu. i'm going to look further into it and try my best to fully understand how it works.
and thanks for catchin that parse error, bud. i caught it too =)
here's my redirect.php. i've got a parse error on line 50 which is my header() function. i don't know what that's about, but i'm sure it will work fine once i get some help with that little issue... let me know what you think.
and thanks for catchin that parse error, bud. i caught it too =)
here's my redirect.php. i've got a parse error on line 50 which is my header() function. i don't know what that's about, but i'm sure it will work fine once i get some help with that little issue... let me know what you think.
Code: Select all
<?php
$ID = $_POST['uID'];
$brideFname = $_POST['brideFname'];
$brideLname = $_POST['brideLname'];
$groomFname = $_POST['groomFname'];
$groomLname = $_POST['groomLname'];
//if address changed, replace the variable value
if(isset($_POST['ship_add'])){
$ship_add = $_POST['ship_add'];
}if(isset($_POST['ship_city'])){
$ship_city = $_POST['ship_city'];
}if(isset($_POST['ship_state'])){
$ship_state = $_POST['ship_state'];
}if(isset($_POST['ship_zip'])){
$ship_zip = $_POST['ship_zip'];
}
//if event dates changed, replace the variable value
if(isset($_POST['event_month'])){
$event_month = $_POST['event_month'];
}if(isset($_POST['event_day'])){
$event_day = $_POST['event_day'];
}if(isset($_POST['event_year'])){
$event_year = $_POST['event_year'];
}
@ $db = mysql_connect("localhost", "apache", "nopass");
mysql_select_db("registry_DB", $db);
if(!$db){
echo "Error: Could not connect to the database. Please try again later.";
exit;
}
$sql = "UPDATE my_search_table ".
"SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
"groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
"event_year = '$event_year' ".
"WHERE uID = '$ID'";
mysql_query($sql) or die(mysql_error());
if(!mysql_query($sql)){
$status = "<font color=red>Your data has not been stored</font>";
}else{$status = "<font color=red>Your data was successfully stored.</font>";
mysql_close($db);
$_POST['status'];
header("Location: /../../../../editCouple.php?editID=". $ID ."");
exit;
?>ok a couple of issues. the event_dates are being deleted as soon as i click save... probably because i don't have an elseif() statement in my redirect.php for the event_dates. and when i try to edit the address information, it doesn't change, but it doesn't get deleted either... unlike the event_date... other than those two problems... this thing works perfecty...
any suggestions?
any suggestions?