PHP Update content chosen in a drop down list in a form on edit

I am trying design a form which involves a drop down list. I would like to edit the form by changing the value of the variables in the form. I have retrieved all the values from mysql DB, but when I click the submit button without changing the value in the dropdown list. The value will be set to null.

if(isset($_POST['update'])){

    $update_id = $_GET['edit_form'];
    $post_title1 =  $_POST['title'];
    $post_date1 = date("Y-m-d");
    $post_author1 = $_POST['author'];
    $post_keywords1 = $_POST['keywords'];

    $post_type1 = $_POST['category']; // This is the content I want to update but was               set to null, the rest is fine.

    $post_content1 = $_POST['content'];
    $post_image1 = $_FILES['image']['name'];
    $image_tmp = $_FILES['image']['tmp_name'];
}}

This is my dropdown list:

<html>
<select name="category" class="err">`enter code here`
<option value=""><?php echo $post_type;?></option>
<option value="Class">Class</option>
<option value="Facilities">Facilities</option>
<option value="Services">Services</option>
</select>

What I want is when I click the submit button without changing the content of the post, the $post_type1 will not be set to null, but keep its original value from DB.

Thanks, hope I have stated my question clearly:)

Here's the code that builds the db query :

$post_image1 = $_FILES['image']['name'];
move_uploaded_file($image_tmp,"../images/$post_image1");
$update_query = "UPDATE post SET post_title='$post_title1', post_date='$post_date1',       post_author='$post_author1', post_image='$post_image1', post_keywords='$post_keywords1',post_type='$post_type1',
post_content='$post_content1' WHERE post_id ='$update_id'";

In HTML you should set a default value for the dropdown.

You write a select element with options. The current database value's option element should have the boolean selected attribute set.

How can I set the default value for an HTML <select> element?

<select name="category" id="category">
    <option></option>
    <option></option>
    <option selected="selected">

    </option>
    <option></option>
</select>

This way $post_type1 will hold the selected value if you click submit without modifying the form.

Now for the image. you have to test values to build the right mySQL query:

$update_query = "UPDATE post SET post_title='$post_title1', post_date='$post_date1', post_author='$post_author1'";
$post_image1 = isset($_FILES['image']) ? $_FILES['image']['name'] : false;
if($post_image1 !== false){
    if(move_uploaded_file($image_tmp,"../images/$post_image1")){
        //file successfully moved
        $update_query .= " , post_image='$post_image1'";
    }
}
$update_query .= ", post_keywords='$post_keywords1',post_type='$post_type1',
post_content='$post_content1' WHERE post_id ='$update_id'";

You get the gist of it!?


Best way to do it would be have a default value of the drop down to be like -1 .eg

HTML

<select name="category">

    <option id="-1">Please Select</option>
    <option id="1">Category 1</option>
    <option id="2">Category 2</option>

</select>

PHP

<?php

$post_type1 = ( 0 < ( int ) $_POST[ 'category' ] ) ? $_POST[ 'category' ] : null;

Edit : Sorry, misread the question

This is probably more what you want :

PHP

<?php

$mysqli = new mysqli( "localhost", "xxx", "xxx", "xxx" );

$query = "SELECT * FROM xxx";

$options = array();

$category = ( int ) $_POST[ 'category' ];

$query = $mysqli->query( $query );

if( FALSE !== $query )
{
    while( $row = $mysliResults->fetch_assoc() )
    {
        $selected = "";

        if( $category === ( int ) $row['id'] )
            $selected = " selected="selected"";

        $options []= "<option id="{$row['id']}"{$selected}>{$row['name']}</option>":
    }
}

echo "<select name="category">", implode( "nt", $options ), "</select>";

Hope that helps

链接地址: http://www.djcxy.com/p/88088.html

上一篇: 刷新后从动态下拉列表中维护选择值

下一篇: PHP在编辑的表单上的下拉列表中选择更新内容