Jump to content

[SOLVED] php questionnaire that delivers answers depending on the responses


stupid girl!

Recommended Posts

Hi All

 

I am trying to build a questionnaire that once submitted returns a page that will provide answers and products dependent on wha the response of the individual is.

 

does my form have to be a get rather than post (im presuming it does). and how do i sort the conditions so that if one answer is selected it writes a unique response?

 

thanks

The Get Method is seen in the address bar and available in $_GET array after submitting the form.

 

The Post Method is invisible and available in the $_POST array after submitting the form.

 

Set the action of the form to be the page that will use the values of the $_POST array.

Yes.

 

I usually double-quote my strings when inserting array variables, even standard variables actually, and encapsulate with brackets to signify the insertion of a variable...like this...

 


echo "Hello, {$_POST['name']}.";

 

if you hate the brackets, you can remove the internal single quotes from the $_POST array key like this...

 


echo "Hello, $_POST[name].";

yeah added something like that in already! good to know im on the right lines lol

 

basically ill give you an example. one of my form inputs is the following:

<select name="mattbudget" id="mattbudget">
            <option value="under100">Under £100</option>
            <option value="100-500">£100-£500</option>
            <option value="501plus">£501 plus</option>
            <option value="Please Select One" selected="selected">Please Select One</option>
          </select>

 

so i want the php to write a response dependent on the option selected


Form Code:

<form action="response-generator.php" method="post">
     <select name="mattbudget" id="mattbudget">
            <option value="under100">Under £100</option>
            <option value="100-500">£100-£500</option>
            <option value="501plus">£501 plus</option>
            <option value="Please Select One" selected="selected">Please Select One</option>
     </select>
</form>

 

Response-Generator.php Code:


// Only when content is posted, do this... //
if (!empty ($_POST)){

     if ($_POST['mattbudget'] == '100-500'){

          echo 'Matt has a good budget.';

     }


}

 

For multiple responses, populate a variable and echo it at the finish like this...

 


// Only when content is posted, do this... //
if (!empty ($_POST)){

     $string = '';

     if ($_POST['mattbudget'] == '100-500'){

          $string .= 'Matt has a good budget.<br>';

     }
     if ($_POST['matttaxes'] > 1000){

          $string .= 'Matt has a good tax return.';

     }

     echo $string;

}

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.