<label class='colwidth' for='selectc' >Select Courses*: </label>
<form action="send.php" name="course" id="states"method="post">
<Select name="States" id="State" style="width:40px;" >
  <option value=></option>
  <option value="1">HTML 5</option>
  <option value="2">JQuery</option>
  <option value="android">American Samoa</option>
  <option value="flash">Flash</option>
  <option value="flex">Flex</option>
  <option value="javascript">Javascript</option>
 </Select>
 <input type='submit' name='course' value='Submit' />
</form>

And my php file is this

<?php
//Place state array at the top of the file
$states =  array(
          1 =>   "html5",
          2 =>   "jquery",
          3 =>   "android",
          4 =>   "flash",
          5 =>   "flex",
          6 =>   "javascript"
           );

//Generate select box
if(!is_array($states)) {
  echo "<select name='states' id='states' style='width:40px'>";
foreach($states as $id => $name) {
    echo "<option value='$id'>$name</option>";
  }
  echo "</select>";
}

//Form POSTed
if(is_array($_POST)) {
  //Send email message
  $email = "State: ".$states[$_POST['state']];
  //Do email sending or message displaying

  //Submit to datebase
  //$query = "INSERT INTO table ('state') VALUES ('".mysqli_escape_string($_POST['state'])."')";
  //Do insert into mysql
}
?>

Recommended Answers

All 16 Replies

And what exactly is the problem with this code?

I am running it from a desktop folder
when i select the andoid or html and press submit it gives me this reponce

"; } foreach($states as $id => $name) { echo ""; } echo ""; } //Form POSTed if(is_array($_POST)) { //Send email message $email = "State: ".$states[$_POST['state']]; //Do email sending or message displaying //Submit to datebase //$query = "INSERT INTO table ('state') VALUES ('".mysqli_escape_string($_POST['state'])."')"; //Do insert into mysql } ?>

Or should I say; what is it expected to do, or what error is it showing u?

it is displaying the the code rather than giving the result

I need a user shall select an course type and when he submits that value shall be emailed to the emal adress here atleast it shall give us an option selected in form.It echos the code

Ok, it seems like you are echoing your code wrongly. Lemme first try running it ...

It reciognises this code

if(is_array($states)) {
  echo "<select name='states' id='states' style='width:40px'>";
  }

but when it reaches this code instead of executing it

foreach($states as $id => $name) {
    echo "<option value='$name'>$name</option>";
  }
  echo "</select>";

Firstly, move it from a desktop folder and run it in a local server. Preferably either wamp server or Xampp.
Try that first, then will take it from there.

Coz, for PHP to run, it will require a server-side host.

after submitting form it says

Parse error: syntax error, unexpected ')' in C:\wamp\www\send.php on line 10

LOcal host

I have updated the code a bit and put it on localhost.
My fresh code is as below

<?php
//Place state array at the top of the file
$states =  array(
         1 => "html5",
         2 => "jquery",
         3 => "android",
         4 => "flash",
         5 => "flex",
         6 => "javascript",
          );

//Generate select box
if(is_array($states)) {
  echo "<select name='state' id='state' style='width:40px'>";
  foreach($states as $id => $name) {
    echo "<option value='$id'>$name</option>";
  }
  echo "</select>";
 }

//Form POSTed
if(is_array($_POST)) {
  //Send email message
  $email = "State: ".$states[$_POST['state']];
  //Do email sending or message displaying

  //Submit to datebase
  //$query = "INSERT INTO table ('state') VALUES ('".mysqli_escape_string($_POST['state'])."')";
  //Do insert into mysql
}
?>

and html is as below

<form action="send.php" name="course" id="states"method="post">
<label class='colwidth' for='states' >Select Courses*: </label>
<Select name="states" id="states" style="width:40px;" >
  <option value=></option>
  <option value="1">HTML 5</option>
  <option value="2">JQuery</option>
  <option value="android">American Samoa</option>
  <option value="flash">Flash</option>
  <option value="flex">Flex</option>
  <option value="javascript">Javascript</option>
 </Select>
 <input type='submit' name='course' value='Submit' />
</form>

and error that pops up is as below

 ! ) Notice: Undefined index: state in C:\wamp\www\send.php on line 24
Call Stack
#   Time    Memory  Function    Location
1   0.0016  141600  {main}( )   ..\send.php:0

( ! ) Notice: Undefined index: in C:\wamp\www\send.php on line 24
Call Stack
#   Time    Memory  Function    Location
1   0.0016  141600  {main}( )   ..\send.php:0

Ok, your line ten shows that there is something missing, so edit it to be;

$states =  array(
         1 => "html5",
         2 => "jquery",
         3 => "android",
         4 => "flash",
         5 => "flex",
         6 => "javascript"
          );

where do you see line 10 it says line 24

Thanks all for participating i Soloved this one Myself.Hope we will meet again.As we are sailing in the same boat of PHP.
Here is a succesfull code

<?php
if(is_array($formCourse)) {
  echo "<select name='formCourse' id='formCourse' style='width:40px'>";
  foreach($formCourse as $id => $formCourse) {
    echo "<option value='$id'>$formCourse</option>";
  }
  echo "</select>";
}

if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$parentage = $_POST['parentage'];
$residence = $_POST['residence'];
$email = $_POST['email'];
$tehsil = $_POST['tehsil'];
$district = $_POST['district'];
$sex = $_POST['sex'];
$age = $_POST['age'];
//$submit2 = $_POST['submit2'];
$whwo = $_POST['whwo'];
$contactno = $_POST['contactno'];
$bloodgroup = $_POST['bloodgroup'];
$identificationmark = $_POST['identificationmark'];
$policestation = $_POST['policestation'];
$formCourse = $_POST['formCourse'];
$submit = $_POST['submit'];

//Validate first
if(empty($name)||empty($email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = "busyharsh@embdia.com";//<== update the email address
$email_subject = "New Form submission";
$email_body = "New Form Has been submitted by $name.\n".
    "Here is the message:$parentage.\n".
    "Email:\n $email.\n".
    "Residence: $residence.\n".
    "Phone NO:$phoneno.\n".
    "Qualification:$qualification.\n".
    //"Sex:$sex".
    //"Age:\n $age".
    "Course Selected:$formCourse.\n".

$to = "imti321@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

Glad to be of help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.