Posting a Form and looking for an alternative to die()

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

User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Posting a Form and looking for an alternative to die()

Post 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.");
   }
 
Last edited by edawson003 on Sun Sep 13, 2009 12:08 pm, edited 2 times in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Post a Form and looking for an alternative to die()

Post by Darhazer »

Throw an exception, that catch the exception and show the form again, with the error message from the exception.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Post a Form and looking for an alternative to die()

Post 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
}
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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.
Last edited by edawson003 on Sun Sep 13, 2009 5:21 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Posting a Form and looking for an alternative to die()

Post 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'] : ''; ?>" />
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post by edawson003 »

That's it... Perfect!

Thanks again, pytrin
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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>
Last edited by edawson003 on Sun Sep 13, 2009 6:08 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Posting a Form and looking for an alternative to die()

Post 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>
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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!
Last edited by edawson003 on Sun Sep 20, 2009 4:20 pm, edited 1 time in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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?
Last edited by edawson003 on Thu Sep 17, 2009 10:44 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Posting a Form and looking for an alternative to die()

Post by Eran »

Code: Select all

$_POST = array();
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).
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post 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.
Last edited by edawson003 on Thu Sep 17, 2009 10:53 pm, edited 2 times in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Posting a Form and looking for an alternative to die()

Post by edawson003 »

BTW, I would like the form to post the optionid but have the drop down display the option name. :)
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Posting a Form and looking for an alternative to die()

Post 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>';
 
Post Reply