1

Well, I have the following problem: my little test website is near completion and all that’s left is PHP validation on submit. But there exactly lies my problem. I can’t seem to let it validate when the user clicks submit.

I searched plenty of websites but on all of those they submit the page to the same page, and I submit the page to a new page. I don’t know how different this is from the first method but I can’t seem to get it to work.

I have 5 fields in my form which need to be required:

<table>
    <tr>
        <td align="right">Naam:</td>
        <td align="left"><input type="text" name="naam" onkeydown="return names(event)"/></td>
    </tr>
    <tr>
        <td align="right">Woonplaats:</td>
        <td align="left"><input type="text" name="woonplaats" onkeydown="return names(event)"/></td>
    </tr>
        <tr>
        <td align="right">Straatnaam:</td>
        <td align="left"><input type="text" name="straatnaam" onkeydown="return names(event)"/></td>
    </tr>
    <tr>
        <td align="right">Huisnummer:</td>
        <td align="left"><input type="text" class="formnumbers" name="huisnummer"/></td>
        </tr>
    <tr>
        <td align="right">Telefoonnummer:</td>
        <td align="left"><input type="text" name="telefoonnummer" onkeydown="return phonenumber(event)"/></td>
    </tr>
</table>

That’s all. My form action is as follows:

<form action="?page=pizza_bestelling" method="post" name="orderform">

I know PHP validation should be really easy (at least that’s what I’ve heard) but I can’t seem to work it out myself, so that’s why I decided to ask it here.

Now all it needs to do is check if one of those 5 fields is empty, if so don’t submit. Otherwise submit.

(Keep in mind that I have 16 fields in my form, it counts only for these 5 specific, the other 11 can be empty)

I appreciate any help in this matter, since it’s all that’s left before completing my website!

Thanks in advance!

3
  • 1
    You'll need Javascript to validate form elements before submitting, this can't be done with PHP since it runs on the server, not in your browser. Commented Jan 30, 2014 at 14:33
  • so what is happening though? Where's your server side code? And is the form POSTing correctly? i.e. in the network tab of your console you see the POST going out as a 200? Is all the data from the form in the POST body of the request? What debugging are you doing on the server side? What do you get if you var_dump $_POST? Commented Jan 30, 2014 at 14:34
  • @thescientist when i fill in some information and press submit i get send to pizza_bestelling and i have an array with all the information an foreach to print it on the website, all is working fine Commented Jan 30, 2014 at 14:40

3 Answers 3

1

Upon submiting you will lose "?page=pizza_bestelling" of the link, you can set it in a hidden input with that value if you need it passed.

set the method to method="post" in the form

and on the same page you will need something like

function validate_form($data)
{
   //rules that will end with  return false; if they are not valid
   // example
   if (!isset($data["naam"]) || $data["naam"] == '') return false; // if the name is not set or it's empty

   // return true if all validation checks are passed
   return true;
}


if ($_SERVER["REQUEST_METHOD"] === "POST")
{
   $form_data['naam'] = $_POST['naam'];
   $form_data['woonplaats'] = $_POST['woonplaats'];
   // and so forth for each variable

   // run the validate_form function for the data you got through POST
   validate_form($form_data);

}else
{
 //form displayed here

}
Sign up to request clarification or add additional context in comments.

8 Comments

How OP can lose ?page=pizza_bestelling after submitting? It will be in $_SERVER['QUERY_STRING']
if (!isset($data["naam"] || $data["naam"] == '') you seem to miss a ) somewhere am i right? my code won't work on that line, gives an error
@ voodoo417 try the code, he has a query string in it, not an actual php file to which it points, wasn't sure myself so I tested it before posting the comment - in his case having action="?page=pizza_bestellin" or action="" it's the same thing.
I don't know if I should be adding something myself except the other variables $form_data since i did that, but when i fill in no fields and press submit i get send to pizza_bestelling anyway. PS: You misspelled METHOD (METHDO)
set it to action="<?php echo $_SERVER[PHP_SELF];?>" and also add in the form <input type="hidden" name="page" value="pizza_bestselling" unless that page does exists and you are using a MVC framework like (ci/smarty/zend) in which case you are doing it completly wrong :)
|
0

You want to set up an errors array which will store any errors, and allow you to print them out if necessary.

Then you check each variable in the following manner:

$errors = array(); //initialise error array

if (empty($_POST['naam'])) {
        $errors[] = 'You forgot to add a name';
    } else {
        $name = trim($_POST['naam']);
    }

This checks if the naam input and writes a message to the errors array if it's empty, or assigns the value to the $name variable if not.

Once you've gone through all your inputs, you can then perform a check on the errors array:

if (empty($errors)) { // If everything's OK.
 //do what you need to do
} else {
echo '<p>The following errors occurred:<br/>';
        foreach ($errors as $msg) {
            echo "$msg<br/>\n";
        }
        echo '<p>Please try again</p>';
    }

This only checks for empty fields of course - you should implement more validation for valid emails etc. For useability purposes you should really look at a one-page solution with sticky fields though.

Comments

0

Do a for each as follows:

$allowed = array("naam","woonplaats","stratnaam","formnumbers","telefoonnummer");
foreach ($allowed as $key => $val){
  if($_POST[$key] == "")
    $errors = true;
}
if($errors){
  // don't submit
}
else {
  // submit 
}

1 Comment

I feel kinda stupid for asking, but what do i need to fill in within the don't submit and submit? =S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.