Jump to content

Very Basic Question - Collecting Values From Form


joshspaulding

Recommended Posts

The required attribute is not respected by all browsers, ie. IE <=9, for example. I believe it falls within HTML5 capable browsers, so don't rely on it.

 

hmm ok. Is there another html option that is respected by all browsers? Or, a PHP option? The option you mentioned seemed to just echo a statement on the results page.?

Link to comment
Share on other sites

hmm ok. Is there another html option that is respected by all browsers? Or, a PHP option? The option you mentioned seemed to just echo a statement on the results page.?

 

I was just giving you an example. A best-case scenario, IMO, is a combo of javascript and PHP. The javascript/jQuery for instant notice that there are errors on the form, and the PHP (backend support) for those who have javascript disabled or have malicious intentions.

 

For the PHP, since we are in the PHP forum, can be something as simple as:

 

<?php
if (isset($_POST['submit'])) {

   $errors = array();

   if (empty($_POST['q1'])) {
       $errors['q1'] = 'Please enter a response to question 1.';
   }
   if (empty($_POST['q2'])) {
       $errors['q2'] = 'Please enter a response to question 2.';
   }
   if (empty($_POST['q3'])) {
       $errors['q3'] = 'Please enter a response to question 3.';
   }

   if (empty($errors)) {

       $qone = $_POST['q1'];
       $qtwo = $_POST['q2'];
       $qthree = $_POST['q3'];

       if ($qone == "q1a"){
           echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";
       }
       elseif ($qone == "q1b"){
           echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";
       }
       if($qtwo == "q2a"){
           echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";
       }
       elseif ($qtwo == "q2b"){
           echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";
       }
       if ($qthree == "q3a"){
           echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";
       }
       elseif ($qthree == "q3b"){
           echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
       }
   }
}

if (!empty($errors)) {
   echo 'There are errors in your form.  Please see below:<br/><br/>';
   $i = 1;
   foreach ($errors as $error) {
       echo "{$i}. $error<br/>";
       $i++;
   }
}
?>
<style type="text/css">
.error {
   color:red;
   display:block;
   }
</style>

<form name="questions" action="" method="post">
   <h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
   <?php echo (!empty($errors['q1']) ? '<div class="error">'. $errors['q1'] .'</div>' : ''); ?>
   <input type="radio" name="q1" value="q1a"<?php echo (($_POST['q1'] == 'q1a') ? ' selected="selected"' : ''); ?>/> : I am pro-life<br />
   <input type="radio" name="q1" value="q1b"<?php echo (($_POST['q1'] == 'q1b') ? ' selected="selected"' : ''); ?>/> : I am pro-choice

   <h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
   <?php echo (!empty($errors['q2']) ? '<div class="error">'. $errors['q2'] .'</div>' : ''); ?>
   <input type="radio" name="q2" value="q2a"<?php echo (($_POST['q2'] == 'q2a') ? ' selected="selected"' : ''); ?>/> : I support gun rights<br />
   <input type="radio" name="q2" value="q2b"<?php echo (($_POST['q2'] == 'q2b') ? ' selected="selected"' : ''); ?>/> : I believe citizens should be unarmed

   <h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
   <?php echo (!empty($errors['q3']) ? '<div class="error">'. $errors['q3'] .'</div>' : ''); ?>
   <input type="radio" name="q3" value="q3a"<?php echo (($_POST['q3'] == 'q3a') ? ' selected="selected"' : ''); ?>/> : I support free markets<br />
   <input type="radio" name="q3" value="q3b"<?php echo (($_POST['q3'] == 'q3b') ? ' selected="selected"' : ''); ?>/> : I support regulating to any extent necessary<br />
   <br />
   <input type="submit" value="Tell me if I'm a Republican or Democrat!" name="submit" />
</form>

 

Offered two examples (can be used in conjunction with each other, or separately).  You can fart around with the styling, positioning, etc.  As well as adding any additional conditions within your form processing code.

 

EDIT: Replace selected="selected" with checked="checked"

 

<form name="questions" action="" method="post">
   <h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
   <?php echo (!empty($errors['q1']) ? '<div class="error">'. $errors['q1'] .'</div>' : ''); ?>
   <input type="radio" name="q1" value="q1a"<?php echo (($_POST['q1'] == 'q1a') ? ' checked="checked"' : ''); ?>/> : I am pro-life<br />
   <input type="radio" name="q1" value="q1b"<?php echo (($_POST['q1'] == 'q1b') ? ' checked="checked"' : ''); ?>/> : I am pro-choice

   <h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
   <?php echo (!empty($errors['q2']) ? '<div class="error">'. $errors['q2'] .'</div>' : ''); ?>
   <input type="radio" name="q2" value="q2a"<?php echo (($_POST['q2'] == 'q2a') ? ' checked="checked"' : ''); ?>/> : I support gun rights<br />
   <input type="radio" name="q2" value="q2b"<?php echo (($_POST['q2'] == 'q2b') ? ' checked="checked"' : ''); ?>/> : I believe citizens should be unarmed

   <h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
   <?php echo (!empty($errors['q3']) ? '<div class="error">'. $errors['q3'] .'</div>' : ''); ?>
   <input type="radio" name="q3" value="q3a"<?php echo (($_POST['q3'] == 'q3a') ? ' checked="checked"' : ''); ?>/> : I support free markets<br />
   <input type="radio" name="q3" value="q3b"<?php echo (($_POST['q3'] == 'q3b') ? ' checked="checked"' : ''); ?>/> : I support regulating to any extent necessary<br />
   <br />
   <input type="submit" value="Tell me if I'm a Republican or Democrat!" name="submit" />
</form>

Edited by mrMarcus
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.