Skip to content Skip to sidebar Skip to footer

Why Does The Php Not Work For My Form?

I have been trying to make this form validate before sending an email to my company. It will not validate the form, it will just send it whenever someone clicks submit. No valida

Solution 1:

There are a few things that need to happen in order for your PHP to read the form data. First, as Waygood suggested, it is a good idea to change your $_POST values to have single quotes around the passed variable name.

//For example:$_POST[address]; 

// Should change to:$_POST['address']; //Notice the single quotes around the word address

Secondly, as answered by John, there is no reference to the passed submitted value in your html form, you'll want to add a hidden input in your form (anywhere between your tags) that holds the value of 1 to make your first statement true in order for the rest of your php code to execute.

<!-- Put this input tag anywhere between your opening and closing form tags in your html --><formmethod="POST"action="your_submission_page.php"><inputtype="hidden"name="submitted"value="1" /></form>

Finally, it's good practice to notify the user of ALL of the errors in the form so that they don't experience error after error before being able to submit the form. What you should be doing is building a dynamic array of error messages, checking to see if there are any errors and then displaying them all.

<?php// Create a dynamic array for the error messages and validate everything before sending them back to the form that shows them// Here is a sample of your code with the dynamic array built in$error_msgs = array ();   
    $error = 0; //Error flag     if ($_POST['cwage2']){
        $cwage2 = $_POST['cwage2'];
    } else {
        $error_msgs[] = "Please enter wage earned, or type N/A";
        $errors++; //Flag to let the code know that there are errors, used in every case
    }

    if ($_POST['cstart2']){
        $cstart2 = $_POST['cstart2'];
    } else {
        $error_msgs[] = "Please enter date started, or type N/A";
        $errors++; //Flag to let the code know that there are errors, used in every case
    }

    // Finally at the end, check to see if there are any errors and display them allif ($errors > 0) {
        //Display the amount of errors firstecho"There were " . $errors . " errors in your submission, please correct them.<br/>";

        //Then display your error arrayforeach ($error_msgsas &$msg) {                
            echo$msg . "<br/>"; 
        }
    } else {
        // Add code here to submit the form however you'd like

    }

?>

Solution 2:

I don't see a form field named submitted and without it your PHP validation code won't be triggered. Try changing:

if ($_POST['submitted']==1) {

to something more generic like this:

if ('POST' === $_SERVER['REQUEST_METHOD']) {

Solution 3:

OK @KibaHeitfield

Change references to array elements from:

$_POST[first_name]

to:

$_POST['first_name']

Solution 4:

  1. there is no if for following else

    else{ if ($errormsg){ //if there is already an error, add next error $errormsg = $errormsg; }else{

    }
    

    }

  2. $errormsg = $errormsg; doesn't mean anything.

  3. Errormsg is assigned but never used. Use echo $errormsg;
  4. $_POST[first_name] should be $_POST['first_name']
  5. Instead of $_POST['submitted'] use if($_POST['field89'] == 'Submit')
  6. Why don't you experiment with a small form and expand when it starts working.

Solution 5:

Or you can simply check this way

if($_POST){
 //form is posted do your work here
}

Post a Comment for "Why Does The Php Not Work For My Form?"