Page 1 of 2
Posting a Form and looking for an alternative to die()
Posted: Sat Sep 12, 2009 2:19 pm
by edawson003
I swipe this code off the net that sets up a user registration form with a couple field validations, but the validation seems to utilize this die() function that kills the form submission and displays the error message only - - - the form element and etc are no longer vidible. However, I would rather the error messages fire by the applicable field input elements, which I think I have a handle on. Is there something else I can use instead of the die() function to stop the form posting while and error message of my choosing fires?
Code: Select all
if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
[color=#FF0040]die[/color]('You didn\'t fill in a required field.');
}
/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']);
if(strlen($_POST['user']) > 30){
[color=#FF0000]die[/color]("Sorry, the username is longer than 30 characters, please shorten it.");
}
Re: Post a Form and looking for an alternative to die()
Posted: Sat Sep 12, 2009 2:55 pm
by Darhazer
Throw an exception, that catch the exception and show the form again, with the error message from the exception.
Re: Post a Form and looking for an alternative to die()
Posted: Sat Sep 12, 2009 3:01 pm
by Eran
or.. collect errors in an array and check at the end if the array is empty or not.
Code: Select all
if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
$errors[] = "You didn't fill in a required field.";
}
/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']);
if(strlen($_POST['user']) > 30){
$errors[] = "Sorry, the username is longer than 30 characters, please shorten it.";
}
if(empty($errors)) {
//process form
}
}
if(isset($errors) && ! empty($errors)) {
//Show errors
}
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 4:41 pm
by edawson003
Both great suggestions, pytrin and Darhazer. Thank you much! I went with the array collection method.
When the form is submitted with certain errors that prevent the post, I would like the form to refesh with the previously entered field values retained. Right now the all seem to clear after pressing submit. I have a feeling that might be moving into Javascript territory.
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 4:48 pm
by Eran
You don't need javascript - or rather, javascript can't help you after the page has been refreshed. check for the existence of a POST value for each of the inputs. For example:
Code: Select all
<input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" />
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 5:01 pm
by edawson003
That's it... Perfect!
Thanks again, pytrin
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 5:16 pm
by edawson003
How would I apply
Code: Select all
value="<?php echo isset($_POST['field1']) ? $_POST['field1'] : ''; ?>" />
to a drop down field?
like:
Code: Select all
<select name="field1" size="1">
<option value="000">select0</option>
<option value="001">select1</option>
<option value="002">select2</option>
</select>
I tried this
Code: Select all
<select name="field1" size="1" value="<?php echo isset($_POST['field1']) ? $_POST['field1'] : ''; ?>" />
<option value="000">select0</option>
<option value="001">select1</option>
<option value="002">select2</option>
</select>
but pre-submit selection wasn't retained.
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 5:20 pm
by edawson003
What about text area type form elements as well? I tried this:
Code: Select all
<textarea type="text" name="description" maxlength="8000" cols="60" rows="4" value="<?php echo isset($_POST['description']) ? $_POST['description'] : ''; ?>" /></textarea>
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 5:30 pm
by Eran
The select input needs the specific option to be marked as selected.
Code: Select all
<option value="35" selected="selected">35 cans of whoop ass</option>
The only way to achieve this with PHP with any reasonable maintainability is to iterate over an array. Something like:
Code: Select all
$options = array(15,19,24,28,50); //All the values of the select;
$selectedValue = isset($_POST['someselect']) ? $_POST['someselect'] : '';
echo '<select name="someselect">' . "\n";
foreach($options as $value) {
echo '<option value="' . $value . '" ' . ($value == $selectedValue ? 'selected="selected"' : '') . '>' . $value . '</option>' . "\n";
}
echo '</select>';
Most people build functions to handle those kind of repetitive form element building.
Textareas are similar to regular inputs, only the data goes between the textarea tags:
Code: Select all
<textarea name="greattext"><?php echo isset($_POST['greattext']) ? $_POST['greattext'] : ''; ?></textarea>
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 8:12 pm
by edawson003
The suggestion for the text box is pretty straight forward. Easy enough, I will apply the syntax for that. Now the option for setting up an array, looks a little more involved. I'll give it a shot. Thanks!
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 8:42 pm
by edawson003
Okay, so now I got most of my form fields to retain values once post submit is selected and page refreshes. How do I make a condition that once a form is success fully submitted all post data points are reset to empty?
Re: Posting a Form and looking for an alternative to die()
Posted: Sun Sep 13, 2009 8:44 pm
by Eran
Or simply redirect to another page (success page or something similar). That is usually the preferred option (so the user won't refresh and resend the form).
Re: Posting a Form and looking for an alternative to die()
Posted: Thu Sep 17, 2009 10:22 pm
by edawson003
Alright, I applied the header redirect approach and it worked well. Also, tested the Array drop down solution and it seems to work. Is there anyway I can store the selectable options in mysql table and have the Array pull the selecable options from the table.
Table stucture will probably be general, like:
optionname optiontypeid optionid
----------- ------------ -------
flowers 32 001
shovel 32 002
plant seeds 32 003
I want to eventually have an administration page/tool that a power user can use to add and remove drop down options.
Just wanted to know how to setup the option code to point to a table.
Many thanx to all for your assistance so far.
Re: Posting a Form and looking for an alternative to die()
Posted: Thu Sep 17, 2009 10:28 pm
by edawson003
BTW, I would like the form to post the optionid but have the drop down display the option name.

Re: Posting a Form and looking for an alternative to die()
Posted: Thu Sep 17, 2009 11:52 pm
by dude81
Make the options table as this
Code: Select all
$options = array('15'=>'DisplayName1','19'=>'DisplayName2','24'=>'DisplayName3','28'=>'DisplayNam4','50'=>'DisplayName5');
And change the foreach loop
Code: Select all
$selectedValue = isset($_POST['someselect']) ? $_POST['someselect'] : '';
echo '<select name="someselect">' . "\n";
foreach($options as $key=>$value) {
echo '<option value="' . $key. '" ' . ($key== $selectedValue ? 'selected="selected"' : '') . '>' . $value . '</option>' . "\n";
}
echo '</select>';