Jump to content

help with form value php


jagz

Recommended Posts

Hi there, im trying to get value from form to PHP but its not working.

 

 

 

 

<?php

 

 

if (isset($_POST['cartype'])==0)

{

 

echo("Please select car type.");

}

else if (isset($_POST['carcolour'])==0)

{

 

echo("Please select car colour.");

}

else if (isset($_POST['carenginesize'])==0)

{

 

echo("Please select car engine size.");

}

else if (isset($_POST['terms'])==0)

{

 

echo("Please agree to site terms and conditions before submitting your order.<br/>

 

Press the back button in your browser to try again.");

}

 

else{

 

 

 

 

$name=$_POST['firstname'];

$lastname=$_POST['lastname'];

$email=$_POST['emailaddress'];

$phone=$_POST['phone'];

 

$ctype=$_POST['cartype'];

$colour=$_POST['carcolour'];

$enginesize=$_POST['carenginesize'];

$options=$_POST['options'];

 

 

echo "Dear,"."$name"."$lastname";

echo "<br/>Thank you for your order";

 

 

echo "<br/>You're contact details are:-";

echo "Phone : "."$phone";

echo "email: "."$email";

 

echo "<br/>You have selected the following car:";

echo "<br/>Car type:"."$ctype";

echo "<br/>Colour:"."$colour";

echo "<br/>Engine size:"."$enginesize";

echo "<br/>Options:<br/>";

 

 

 

$count=count($options);

 

for($i=0;$i<$count;$i++)

{

 

echo $options[$i]."<br/>";

 

 

}

 

 

$optn=array("6 disc DVD changer",

"TV function",

"DAB digital radio",

"Loudspeaker system - professional ",

"Multimedia navigation system",

"Universal remote control",

"Voice control",

"Navigation system",

"Head-up display",

"Adaptive headlights",

"Night vision with pedestrian recognition",

"Run-flat tyres");

 

foreach($options as $optn)

{

echo "NEw option  ".$optn."<br/>";

 

 

}

 

 

 

 

}

?>

 

 

Link to comment
Share on other sites

You'd better go for a different approach on error handling. As you have it, you show just one error, even if there are more. Take a look:

 

<?php
//check if the form has been submitted
//$_POST['submit'] is the post value of the submit button (if you have one)
if (isset($_POST['submit'])) {
     $errors = array();
     if ($_POST['cartype'] != '') { $errors[] = 'Please select car type'; }
     if ($_POST['carcolour'] != '') { $errors[] = 'Please select car colour'; }
     //and so on for every input you need validated

     //if no error occurred, process the form
     if (count($errors) == 0) {
          //process the form
     } else {
          //print errors
          foreach ($errors as $error) {
               echo $error . '<br />';
          }
     }
}
?>

 

Also, keep in mind that isset() checks if a variable exists. In the case of POST, variables will be created and return true to isset(), even if they're empty. It's better to check for length with strlen() or value.

Link to comment
Share on other sites

Thank you replay....can i ask you one more thing...i have multiple value in html form where user can select if they want to...how can i get those value in php if user has selected.?

 

<select class="select" id="extras" name="options[]" multiple="multiple" size="6">
<optgroup label="Entertainment">
<option value="1">6 disc DVD changer</option>
<option value="2">TV function</option>
<option value="3">DAB digital radio</option>
<option value="4">Loudspeaker system - professional </option>
<option value="5" title="Multimedia navigation system. Uses advanced audio technology">Multimedia navigation system</option>
</optgroup>
<optgroup label="Communication">
<option value="6">Universal remote control</option>
<option value="7">Voice control</option>
<option value="8">Navigation system</option>
<option value="9">Head-up display</option>
</optgroup>
<optgroup label="Safety and Security">
<option value="10">Adaptive headlights</option>
<option value="11">Night vision with pedestrian recognition</option>
<option value="12">Run-flat tyres</option>
</optgroup>
</select>

 

above is my html code

Link to comment
Share on other sites

You can use data from a drop-down in the same way as in other form elements. In your case:

 

<select name="options">
     <option value="1">6 disc DVD changer</option>
     <option value="2">TV function</option>
     <option value="3">DAB digital radio</option>
</select>

<?php
echo $_POST['options'];
?>

 

The selected value of the drop-down will be echoed. You don't need to treat the <select> as an array (name="options[]") because it can return just one value. You will need that instead when working with checkboxes.

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.