2

I created Err code so that name and radio has to be checked otherwise it can't move on to the confirmation page and send error message next to the field. Please help if I'm missing any code!

<?php
$nameErr = $charityErr = "";
$name = $charity = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is missing";
    }
    else {
        $name = $_POST["name"];
    }
   if (!isset($_POST["charity"])) {
        $charityErr = "You must select 1 option";
    }
    else {
        $charity = $_POST["charity"];
    }
}  
?>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <link type="text/css" href="testStyle.css" rel="stylesheet"/>
<title>Survey</title>
</head>

<body>
<div><!--start form-->
  <form method="post" action="submit.php">
  <input type="radio" name="charity" <?php if (isset($charity) && $charity == "1") echo "checked"; ?> value="1">1 
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "2") echo "checked"; ?> value="2">2 
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "3") echo "checked"; ?> value="3">3
 <span class="error"><?php echo $charityErr;?></span>
  <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
  <span class="error"><?php echo $nameErr;?></span>
  <input type="submit" name="submit" value="Submit"/>     
</form>  
</div><!--end form-->

</body>
</html>

My submit.php says:

/* Subject and Email Variables */
   $emailSubject = 'Survey!';
   $webMaster = '[email protected]';

/* Gathering Data Variables */
   $name = $_POST ['name'];
   $charity = $_POST ['charity'];

    //create a new variable called $body line break, say email and variable, don't leave any space next to EOD - new variable $body 3 arrows less than greater than, beyond EOD no spaces
   $body = <<<EOD
<br><hr><br>
Company Name: $name <br>
Charity: $charity <br>
EOD;

   //be able to tell who it's from
   $headers = "From: $email\r\n";
   $headers .= "Content-type: text/html\r\n";
   $success = mail($webMaster, $emailSubject, $body, $headers); 


/* Results rendered as HTML */
   $theResults = <<<EOD
<html>
blah blah
</html>

This redirects fine to submit.php page except my validation doesn't work and sends blank data.

Form code is above as:

<form method="post" action="submit.php">

  <input type="radio" name="charity"
 <?php if (isset($charity) && $charity == "1") echo "checked"; ?>
 value="1">1 
<input type="radio" name="charity"
 <?php if (isset($charity) && $charity == "2") echo "checked"; ?>
 value="2">2 
<input type="radio" name="charity"
 <?php if (isset($charity) && $charity == "3") echo "checked"; ?>
 value="3">3
 <span class="error"><?php echo $charityErr;?></span>

  <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
  <span class="error"><?php echo $nameErr;?></span>
  <input type="submit" name="submit" value="Submit"/>

</form>  
7
  • pretty sure radio's come out slightly different. What does <?php print $charity; ?> output once you have posted the form? Commented Dec 3, 2014 at 23:41
  • 1
    Can you output echo '<pre>';print_r($_POST);echo '</pre>'; at submit.php? Commented Dec 3, 2014 at 23:45
  • How do you email radio result? Commented Dec 3, 2014 at 23:46
  • Where would you output that line of code? inside the $body @ "submit.php"? Commented Dec 3, 2014 at 23:49
  • You can output at top, just to see what was posted. Commented Dec 3, 2014 at 23:49

1 Answer 1

1

You have a syntax error here:

<?php if (isset($charity) && charity == "3") echo "checked"; ?>

You miss $ in charity var:

<?php if (isset($charity) && $charity == "3") echo "checked"; ?>

About your second question, I think that your form is a little messy. You can use the same page to the form, validation, error management and proccesing, using this structure:

  • capture vars

  • validating

  • proccesing

  • show errors if any or success msg

  • render form if error or not sent

Try some like this:

<?php

//Capture POST/GET vars
$charity = $_REQUEST['charity'];
$name = $_REQUEST['name'];
$step = $_REQUEST['step'];

//You can add some sanitizing to the vars here


//Form sent if step == 1
if ($step == 1){

    /*
     * Validate form
     */

    //Initialize error's array
    $error = array();

    //No charity value error
    if (!$charity){
        $error[] = 'You must select 1 option';
    }

    //Missing name error
    if (!$name){
        $error[] = 'Name is missing';
    }

    //Add any other validation here



    /*
     * Process form if not error
     */

    if (!$error){

        //Send eMail
        $subject = "Your subject here";
        $toemail = "<[email protected]>";
        $bounce = "<[email protected]>";
        $message = "
                   Company Name: $name<br>
                   Charity: $charity <br>";
        $subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?=';
        $headers = "From: <[email protected]>" . "\r\n" .
        "Reply-To: <[email protected]>" . "\r\n" .
        "X-Mailer: PHP/" . phpversion();
        mail($toemail, $subject, $message, $headers, "-f $bounce" );

        //Add any other proccesing here, like database writting

    }

    /*
     * Show errors || succsess msg on the top of the form
     */

    if ($error){
        unset($step); //if error, show the form
        echo '<div style="color:yellow;background-color:red;">ERROR:<br>';
        foreach ($error as $e){
            echo '- '.$e.'<br>';
        }
        echo '</div><br>';
    }else{
        echo '<div>Form succesfully sent</div><br>';

    }



}


/*
 * Form rendering
 */

if (!$step){
?>

<form method="post" action="">
    <input type="radio" name="charity" value="1" <?php echo ($charity == "1") ? ' checked':''; ?>>1 
    <input type="radio" name="charity" value="2" <?php echo ($charity == "3") ? ' checked':''; ?>>2 
    <input type="radio" name="charity" value="3" <?php echo ($charity == "3") ? ' checked':''; ?>>3
    <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
    <input type="hidden" name="step" value="1">
    <input type="submit" name="submit" value="Submit"/>     
</form>


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

2 Comments

You are a hawk! I did not see that AT ALL. Now Email works thank you soooooo much! My next concern was validating, I already placed Err code but when user clicks on empty name it still sends blank data and goes to the "submit.php" page. Which part of code am I missing?
As the form submit is loading another page, you can't avoid empty form submission, unless you use javascript. You can fix it submitting to the same form page using a conditional to stop form processing if values are not validated. Also, you can show an error in next page and a back button or link, but it is less fancy.

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.