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
https://forums.phpfreaks.com/topic/121933-solved-help-with-a-simple-script/
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>

 

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!

 

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...
}

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.

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 ;)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.