Hi i am working on php and mysql.
I have a form where in i am accessing data from one table and upon selection i am inserting that data in to another table. But my major constraint is the selected data id is being stored instead of the data value.
kindly let me know how to get the data value instead of the data id.
Below is the sample code.
<td>Status:</td>
<td> <select name="status" id="status">
<?php $svar = mysql_query("select * from status");
while($sresult = mysql_fetch_array($svar)){ ?>
<option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
<?php } ?>
</select> </td>
Now i am inserting the data from the above form in to another database using the following query:
$sql="INSERT INTO Createticket (......,status) VALUES(..........,'$status')";
Although you left out quite a chunk of your code which would have helped check 100%, I think I can make an educated guess at what your attempting.
It's simply because you are setting the value of the select box as the id, not the value....
Change this line:
<option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
to:
<option value="<?php echo $sresult['status']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
That way, when you post the form, you are posting the value of the field 'status', as the status value not the id value of $result.
the value you are assigning to the option field is $sresult['id'] so the same id is stored in the second table to set the status
replace the
value = " <?php echo $sresult['id'] ?> "
with in oprtions filed to
value = " <?php echo $sresult['status'] ?> "
Related
I have the following code:
<form action="MovieDatabase.php" method='post'>
<select name="director_movie">
<?php
$sql = mysqli_query($mysql, "SELECT DISTINCT Title FROM Film2");
while ($row = $sql->fetch_assoc()){
?>
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
<?php
// close while loop
}
?>
</form>
For the drop-down, which uses the values from SQL table. So the drop-down has values as movie titles.
When I try to access the selected value from MovieDatabase.php like that:
$_POST['director_movie'];
and when I try to echo it, it only shows me option value direcotr_movie1 which is not what I'm looking for. In this case I want to get the title of the movie, which was selected from the dropdown.
What am I doing wrong?
You hard-coded same value for each option in your code in this line:-
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
That's why you are getting same-value every time.
Change it to below:-
<option value="<?php echo $row['Title'];?>"><?php echo $row['Title'];?></option>
Now it will work fine.
Note:- id need to be unique for each HTML element so remove that repetitive id (i don't think you have any need of that)
Change this
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
to
<option value=<?php echo $row['Title']; ?> id="film_id"><?php echo $row['Title']; ?></option>
you are hardcoding your value <option value="director_movie1" id="film_id"> and wondering why your form submits your hardcoded value?
The movie title that is shown in your select is just what the users can see. your form submits what you put into value=<your required information here>
On another note: Using the title as the field might result in problems when using anything but chars / numbers as the title when you want to fetch the movie information based on the selected title. Better use the ID of your movie
1st : Change your value attribute to value="<?php echo $row['Title']; ?>"
2nd : Id attribute is unique for each element. remove it from option tag id="film_id"
<option value="<?php echo $row['Title']; ?>" ><?php echo $row['Title']; ?></option>
change below line
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
with
<option value="<?php echo $row['Title']; ?>" ><?php echo $row['Title']; ?></option>
Now you will get dynamic value different options also id can not have same value for all
I want to send more than one value in the POST but use only one type of input tag.
For example, is it possible to set the value of the option tag to both $data['username_id] and $data['name'] and send it via the POST
<select name="owner" id="owner">
<option value="NULL" selected="selected"></option>
<?php
$sql = 'SELECT * FROM users'
. ' ORDER BY name';
$query = mysqli_query($connect,$sql) or die (mysqli_error($connect));
while ($data = mysqli_fetch_array($query))
{
?>
<option value="<?php echo $data['user_id']; ?>"><?php echo $data['name']; ?></option>
<?php
}
?>
</select>
I can't seem to retrieve on the other end. I have tried:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
But still no luck.
Any help would be useful.
For example, is it possible to set the value of the option tag to both $data['username_id]' and $data['name'] and send it via the POST
Yes, this can be done, carefully.
Working with your example:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
Can be simply rewritten as:
<option value='<?php echo $data['user_id']."--".$data['name']."'>"
.$data['name']; ?></option>
At the other end:
On the form receiver PHP page:
$parts = explode("--",$_POST['owner']);
/***
* $parts[0] = user_id
* $parts[1] = name
***/
BUT as you are grabbing the data from a database row anyway, this is fairly pointless, you might as well JUST transport the user ID and simply use it on the receiving end to grab the 'name' data value from the Database.
There is also potential issues if the splitter (--) appears more than once, so be carefully to choose a splitter that does not apper in either of the values you are trying to send.
Working Freehand:
<input name="whatever[]" value="one">
<input name="whatever[]" value="two">
The square bracket means that the data passed in $_POST will be an array of values.
As pointed out in comments by Kainaw you can also simply use the multiple selection to reach the same effect in your <select> input.
I couldn't keep the selected value after loading the page, that's why I couldn't take the selected value from the select option in Database after done some function..
Here is my code
<select name='courseID' class="mySelect" id='courseID'>
<?php while ($row1 =mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1['CourseID'];?>"><?php echo $row1['CourseID'];?></option><?php endwhile; ?></select>
<script type="text/javascript">document.getElementById('courseID').value="<?php echo $_GET['courseID'];?>";</script>
Did JS can add in a HTML in this way??
You have add condition to check if the course id is in the result.
<?php while ($row1 =mysqli_fetch_array($result1)):;
$selected = ($_GET['courseID'] == $row1['CourseID']) ? 'selected' : '';
?>
<option value="<?php echo $row1['CourseID'];?>" <?php echo $selected;?>><?php echo $row1['CourseID'];?></option>
<?php endwhile; ?>
I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>
This will hopefully be an easy one, but I'm lacking the skills!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
The above is one of many select in a search module. It returns a dynamic list of options from a query higher up in my page. My goal is to have the option last searched pre-selected. Everything works as intended, but my problem is minimal really; the value of the posted search category is an ID($row->id. What I am hoping to do is use the associated $row->name for display, but keep the id for value so my search function still works.
In other words, I'm hoping to do something like:
<?php echo $row->name; WHERE ID = $_POST['search_category']
Is there an easy way to do that in the above code, or will I need to add a special query at the top of my page, fetching the individual row name that matches the posted id?
Thanks!
EDIT: To simplify, I already have a query that returns row->id and row->name, which I use in a foreach loop to populate my option values and names. I simply need a way, or a line that I can add to my query to also get the value of the row->name that matches the POSTED id.
i would write a short function which gives this functionality even for others applications.
just like
<?php
function($id, $table) {
select ... etc
}
?>
For security Reasons I would suggest to use Prepared Statements or mysql_escape
Hope i could help
Perhaps
SELCET('id', 'name' FROM yourTable WHERE 'name' = $_POST['ID'])
you mean something like this or would you select the dropdown option
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"
<?php if($row->name == $_POST['search_category']) : ?>
selected="selected"<?php } ?>>
<?php echo $row->name; ?>
</option>
<?php endforeach; ?>