简体   繁体   中英

How to update the database if I change the dropdown list element and checkbox value

I have created one form in that I have put one drop down list and group of checkbox.

  1. If user select any item from that drop down list than particular item should updated in database(Ex.first user has select division A from drop down list than the A should enter into database and if after some time same user change division from A to B than in database in place of A data should B when user press update button.

  2. In the group of checkbox if user change (select or deselect) checkbox compare to previous input than respective checkbox value should updated in database.(Ex. If user has checked two checkbox than the particular checkbox value should update in database and after some time same user has checked all three checkbox or only one checkbox and particular change should update in database when user click on update button

Here I have put my code. If anyone know solution than please help.

<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db_new", $connection);
$query=mysql_query("SELECT * FROM student_info where id=17");
$row= mysql_fetch_row($query);
$rollno = $row[1];
$name = $row[2];
$division= $row[3];
$std = $row[4];
$gender = $row[5];
$subject=mysql_query("select * from student_info where id=17");
while($fetch=mysql_fetch_object($subject))
{
    $r=$fetch->subject;
    $sub=explode(",",$r);
}
if(count($_GET)>0){
    $rollno=$_GET['rollno'];
    $name=$_GET['name'];
    $std=$_GET['std'];
    $gender=$_GET['gender'];
    $update=mysql_query("update student_info set roll_no='$rollno', name='$name',
    std='$std', gender='$gender' where id='17'",$connection);
}
mysql_close($connection);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Edit Form</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="jquery%20validate/jquery.validate.min.js"></script>
    <script src="jquery%20validate/additional-methods.min.js"></script>
    <script src="form.js"></script>
</head>
<body>
<form id="editform" method="GET" action="task.php">
    <div>
        <h1>Form</h1>
        <label><b>Roll No:</b></label>
        <input id="rollno" name="rollno" type="text" value="<?php echo $rollno ?>"> <br><br>
        <label><b>Name:</b></label>
        <input id="name" name="name" type="text" value="<?php echo $name ?>"> <br><br>
        <label><b>Division:</b></label>
        <select id="division">
            <option value=" " <?php if (!empty($division) && $division == ' ')  echo 'selected = "selected"'; ?>>--select--</option>
            <option value="A" <?php if (!empty($division) && $division == 'A')  echo 'selected = "selected"'; ?>>A</option>
            <option value="B" <?php if (!empty($division) && $division == 'B')  echo 'selected = "selected"'; ?>>B</option>
            <option value="C" <?php if (!empty($division) && $division == 'C')  echo 'selected = "selected"'; ?>>C</option>
            <option value="D" <?php if (!empty($division) && $division == 'D')  echo 'selected = "selected"'; ?>>D</option>
        </select><br><br>
        <label><b>Std:</b></label>
        <input id="std" name="std" type="text" value="<?php echo $std ?>"><br><br>
        <label><b>Gender:</b></label>
        <input type="radio" name="gender" value="male" <?php if($gender=='male'){ echo "checked=checked";}?>>Male
        <input type="radio" name="gender" value="female" <?php if($gender=='female'){ echo "checked=checked";}?>>Female<br><br>
        <label><b>Subject:</b></label><br>
        <input type="checkbox" name="box[]" class="subject" value="maths" <?php if (in_array("maths", $sub)){echo "checked";}?>>maths<br>
        <input type="checkbox" name="box[]" class="subject" value="science" <?php if (in_array("science", $sub)){echo "checked";}?>>science<br>
        <input type="checkbox" name="box[]" class="subject" value="english" <?php if (in_array("english", $sub)){echo "checked";}?>>english<br>
        <input id="submit" type="submit" value="submit">
        <input id="update" type="submit" value="update">
    </div>
</form>
</body>
</html>

数据库布局

You can use jquery ajax to update the data on select and on change of dropdown.

$.post('page.php',{parameter:value},function(data){
 //operation performed after insertion
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM