Jump to content

[SOLVED] Help with a simple script


bc2013

Recommended Posts

I am pretty new to php so this might be easy, Im not sure. I am trying to put together a simple site where I just need one template for the whole thing. I am trying to make a page with a form that has two text boxes and five drop down menus. Now what I need it to do is go to a page that tells them what they have inputted in the fields and send me an email telling me all the inputs. That part I sort of understand how to do but I need one more thing. I would like to have it tell the person if they have forgotten to input one of the fields and if any of the drop down menus and duplicated or missing. Maybe someone has an example or something. Thanks for your time.

Link to comment
Share on other sites

This is fairly easy. I tend to do this in javascript as the user does not need to leave the page to find out if they are missing anything.

However, a php alternative would having in the action:

 

<?php
if (!$_POST['TEXT1'] && !$_POST['TEXT2'] && !$_POST['RADIO1']){
//error message
}

?>

 

However that will never pinpoint the user's mistake.

If you were to pinpoint the users mistake it's a bit more complicated. I'd use a jsp from a ready done jsp framework and mix it with php to not let the user click submit until everyting is spick & span.

 

Guy above me beat me to it!

 

Link to comment
Share on other sites

just check if the input fields are missing (all inclusive example):

 

<?php
   if(!$_POST['blah']) {
      echo "nothing entered <br />";
   }
?>

<form action = '' method = 'post'>
   <input type = 'text' name = 'blah'>
   <input type = 'submit' value = 'submit'>
</form>

 

 

Surely wouldn't $_POST['blah'] come back true regardless if it exists in the form? I could be wring but if it were me, I'd be checking the string length.

 

if (empty($_POST['blah']) {
  // Error...
}

Link to comment
Share on other sites

This is fairly easy. I tend to do this in javascript as the user does not need to leave the page to find out if they are missing anything.

However, a php alternative would having in the action:

 

<?php
if (!$_POST['TEXT1'] && !$_POST['TEXT2'] && !$_POST['RADIO1']){
//error message
}

?>

 

However that will never pinpoint the user's mistake.

If you were to pinpoint the users mistake it's a bit more complicated. I'd use a jsp from a ready done jsp framework and mix it with php to not let the user click submit until everyting is spick & span.

 

Guy above me beat me to it!

 

 

This is bad practice. You can manipulate the post variables after the user has clicked to post (thereby javascript is useless for validation, but only a means of a quick-validation). You must STILL validate with PHP anyway.

Link to comment
Share on other sites

That won't pinpoint the user's mistake because you're throwing all 3 things into one condition.  If you want to pinpoint it break it up into 3 conditions.  That's not a bit more complicated, nor do you need to be throwing some jsp framework into the mix... and I would not advise using javascript for form validation except as a "bell and whistle" feature for users.  That is, it's *more* convenient for them to have a popup before form submission, but you should still have php verify it, because it's on the server, where people can't circumvent your validation.

 

just check if the input fields are missing (all inclusive example):

 

<?php
   if(!$_POST['blah']) {
      echo "nothing entered <br />";
   }
?>

<form action = '' method = 'post'>
   <input type = 'text' name = 'blah'>
   <input type = 'submit' value = 'submit'>
</form>

 

 

Surely wouldn't $_POST['blah'] come back true regardless if it exists in the form? I could be wring but if it were me, I'd be checking the string length.

 

if (empty($_POST['blah']) {
  // Error...
}

 

Try it and find out ;)

Link to comment
Share on other sites

@Wolphie. !$var returns true if the variable is empty. If the $var is empty it returns false, throw the NOT logical operator and you make it true to trigger the if(). It's the same as checking with empty() or $var == ''. Crayon Violent is right :)

Link to comment
Share on other sites

Thank you all for your input. I still am kinda lost but let me ask you from the example given:

 

<?php

  if(!$_POST['blah']) {

      echo "blah is empty <br />";

  }

  if(!$_POST['blah2']) {

      echo "blah2 is empty <br />";

  }

  if(!$_POST['blah3']) {

      echo "blah3 is empty <br />";

  }

?>

 

<form action = '' method = 'post'>

  <input type = 'text' name = 'blah'>

<input type = 'text' name = 'blah2'>

<input type = 'text' name = 'blah3'>

  <input type = 'submit' value = 'submit'>

</form>

 

Is this valid?

Also what about the five drop down fields if any are duplicated. Does anyone know how to do that?

 

Thank you so much again for your guys help.

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.