0

I have created a php page in which it will display error if no value is selected from Dropdown list in both <select> . I am not able to assign values to the select section.

I need

  1. to check before submit, both of the value of option in Select attribute are not empty or 0
  2. form should be submitted successfully.

Error I am getting as:

  1. form is getting successfully submitted but without select validation and
  2. getting error as Undefined index: while saving data in sql

Here is my code.

  function checkSelect() {
        $(document).ready(function () {
    $("#contact-form").submit(function (e) {
        var a=0;
        if($( "#select1 option:selected" ).val()=='0') 
            {   a=1;
            }
        if(!a=="1")
        {   if($( "#select2 option:selected" ).val()==' ') 
            {
            alert('Select2 required fields is ignored... please check your form' );
            }
        }
        else 
        {alert('select1 required fields is ignored... please check your form' );}

        });
        });}
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">

</head>

<body>

<div id="contact-form"> 
<form id="contact" class="demo" method="post" action="">
<fieldset> 

<label for="select1"><span>select1 <span class="required">*</span></span>
<select id="select1" name="select1" class="option3" class="required">
  <option value="0" disabled selected style="display: none;">....</option>
  <option id="select1" value="option0">option0</option>
        <option id="select1" value="option1">option1</option>
        <option id="select1" value="option2">option2</option>
</select>
</label>

  <label for="select2" style= "margin-left: 40px;" ><span style= "width: 70px;">select2<span class="required">*</span></span>
<select class="option3" name="select2" id="select2" class="required">
        <option value="0" disabled selected style="display: none;">....</option>
  <option value="option0">option0</option>
        <option value="option1">option1</option>
</select>
</label>

<label style="margin-left: 37%;padding: 5px;">
<input type="submit" name="submit" class="button" id="submit" value="Submit" onclick="checkSelect(this);"></label>
</fieldset>

</form>
</div><!-- /end #contact-form -->
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO `inputs`(select1,select2) 
VALUES ('".$_POST["select1"]."','".$_POST["select2"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>;";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>;";
}

$conn->close();
}
?>

</body>
</html>
Mr Lister
  • 44,061
  • 15
  • 107
  • 146
Prayag
  • 145
  • 13
  • You are also vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Sep 27 '16 at 14:45
  • hi Marc B , there two select attributes, and i am not able to assign values from id=select1 and id=select2. there are also inout, textbox and checkbox in my Original PHP code, which are working well and i am able to save in database via sql. – Prayag Sep 27 '16 at 14:48
  • pl help........ – Prayag Sep 28 '16 at 07:00

0 Answers0