www.webdeveloper.com
Results 1 to 5 of 5

Thread: selected option in php

  1. #1
    Join Date
    May 2006
    Posts
    48

    selected option in php

    Hi I am having this form for inserting some content. The form contains a select element. what I want to do is to have a similar form with the inserting one for updating the content. My problem is that when I retrieve data from the database and load it in the edit form I want the select element to have the right option selected. e.g. if i have chosen to color red when i inserted the data to have the color red selected again so that when i click update without changing the color it wont lose the value it had initially. How can that be done???
    Thnx in advance
    Yona T

  2. #2
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    259


    Can u send the code??

  3. #3
    Join Date
    May 2006
    Posts
    48
    for example i have the insert form

    Code:
    <form method="post" action="classes.php">
    <select name=semester>
    <option value=''></option>
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
    </select>
    <submit button></form>
    classes.php is something like
    Code:
    <?php
    .....
    $sql="insert into classes ....semester='$_POST["semester"]'";
    $res=mysql_db_query();
    ?>
    when I click on update class I want to run an sql query and retrieve all data from database that are related to that class and preview the class' edit form as follows
    Code:
    <form action=\"classes.php\" method=\"post\" onsubmit=\"return validate_class();\">
    	<input type="hidden" name="class_id"  value=".$result["id"]." />
    	<table >
    		<tr>
    			<td>
    			Class name (*) </td>
    			<td>
    			<input type="text" name="class_name"  value=".stripslashes($result["name"])."></td>
    		</tr>
    		<tr>
    			<td>
    			Class code (*) </td>
    			<td>
    			<input type="text" name="class_name"  value=".stripslashes($result["code"])."></td>
    		</tr>
                   <tr>
    			<td>
    			Semester (*) </td>
    			<td>
    			<select name=semester>
    <option value=''></option>
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
    </select>
    </td>
    		</tr>
    </table><submit button></form>
    but I want the right semester to be selected in the edit form when I load it. When inserted the class i selected that the class belongs to the 2nd semester. When I click on update class I want the select element to have the right semester selected and not to be blank and waiting for me to choose semester again.

  4. #4
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    259
    Ok...It means u r Loosing the selected option value on page refresh...right??

  5. #5
    Join Date
    May 2006
    Posts
    48
    no I mean that on one hand i have the insert form for inserting the data related to the class and on the other hand I have the update class form where I want to load all the data that i have stored in the database for a particular class. the problem is that when i chose to update a particular lesson free text data such as name, code etc can be loaded in the update form simply by using the value parameter of the input element.
    Code:
    <input type="text" name="class_name"  value=".stripslashes($result["code"]).">
    but on the select element i have no such parameter (as far as i know) for having the right option selected.

    how can i do that?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles