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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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].";

Link to comment
Share on other sites

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

Link to comment
Share on other sites


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;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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