get value of select box without submitting a form - php

hi i want to get the value the user selected from the dropdown menu without submitting a form and save it in a php variable any ideas ??
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row["fullname"];
$options .= '<option value="'.$name.'">'.$name.'</option>';
}
?>
<?php echo $options; ?>
</option>
</select>

I don't think you fully understand how PHP works server side, but to get the value of a dropdown menu without submitting it you'll need Javascript (jQuery makes everything easier). From there you just send an AJAX request using JSON as data format and retrieve it from PHP with json_decode.

Related

Taking a value from an array and storing it into a variable for a SQL search

Perhaps there may be an easier way to do this however, I need the project to select a patient from the drop down menu. Then when the dropdown menu has got a value, the text field needs to take the NHS number from that drop down menu (array) so that it can be posted elsewhere.
<select name="patient" class="textbox" required>
<option value="">Select a patient</option>
<?php
include 'dbconnection.php';
$sql = "SELECT * FROM clients ORDER by firstname ASC";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row["firstname"]." ".$row["lastname"]; ?>">
<?php echo $row["firstname"]." ".$row["lastname"] .", ".$row["addressl1"].", ".$row["addressl2"].", ".$row["county"].", ".$row["postcode"].", ".$row["nhsnum"]; ?></option>
<?php
$nhs = $row['nhsnum'];
}
?>
</select>
<?php
$sql = "SELECT * FROM clients WHERE nhsnum = $nhs ";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<input type="text" placeholder="NHS number" readonly value=" <?php echo $row["nhsnum"]; ?>">
<?php
}
?>
As you may see, I have created dummy variables of $nhs however its a static variable and doesnt change upon user selection from the drop down list. Can anyone explain how I can merge the two together.
DB setup
i think you should declare the $nhs outside the while loop
Use AJAX, as already suggested, or a form submit button. Your second query should be where your AJAX or submitted form goes. Use $_GET or $_POST, if you are using get or post method, to intercept the option value. Assign that value to your $nhs, then use as you have.
Set the option value to the $nhs value you want, not the person’s name. Example using $_POST
if(isset($_POST['patient'])){
$nhs=$_POST['patient'];
}else{
// whatever code you want to handle a non-submitted value.
}
Add additional code to prevent SQL injection.

How to data from tabe to combobox dynamically

how can i put the $CATEGORY dynamically so that whatever i click on the table it will retrieved in the combo box? (without settng its id to any number like 5 )
<?php
$CATEGORY = 5; //from DB table, consider 5 as category id for sample
$sql="SELECT tblcourse.id as id, tblcourse.course as course FROM tblcourse";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["course"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>";
}
?>
My Combobox form code below :
<select name="cbocourse" style="height:35px; width:280px; background-color:#923227; box- shadow:1px 1px #FFF;color:#C90;" onClick="submitCATEGORY();">
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option></select>
Your combobox code is wrong : you cannot parse a list of option into another super option, it means nothing. Just parse your $options between your select tags.
Just be sure to reload your page (or page fragment with AJAX for example) each time you call submitCATEGORY(), in order to regenerate your html combobox.
Your PHP code seems good.

how to pass dropdown dynamic value in php/mysql

i have a dropdown list the value of each item generate dynamic
<?php
include('../db_inc.php');
$sql="select * from genre";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($result)){
$option =$row->genre_name;
$value =$row->genre_id;
echo '<option value='.$value.'>'.$option.'</option>';
}
?>
i want pass selected item value from dropdown to server and save that to database,
i can do that with ajax,but i want to know php can do that without ajax function?
PHP runs on server side. Dropdown list on client side. You need somehow send the value from client to server (post, get, ajax, websocket, etc).
The answer is NO.
you can send it with a <form> tag and submit form with on dropdown onchange attribute. then get it with php and save that to database.
<form name='form1' method='post' action=''>
<?php
include('../db_inc.php');
$sql="select * from genre";
$result = mysql_query($sql) or die(mysql_error());
echo '<select onchange="form1.submit();">';
while($row = mysql_fetch_object($result)){
$option =$row->genre_name;
$value =$row->genre_id;
echo '<option value='.$value.'>'.$option.'</option>';
}
echo '</select>';
?>
</form>

How to use assign html select value to a php variable

<select name="parentgroup" type="text" id="select2" onchange="this.form.submit();">
<?php
echo "<option value=\"0\">All Users</option>";
$result = mysql_query("select * from login WHERE stato=1");
while ($myresults = mysql_fetch_array($result))
{
$username = $myresults['user_name'];
echo "<option value=\"".$username."\" ";
echo $username== $parentgroupid ? " selected" : "";
echo ">".$username."</option>";
}
?>
</select>
Hi...am supposed to use the first element in <select> which is <option value=\"0\">All Users</option> to fetch values from a mysql database.
I want to now how you can assign this to a variable and use it just like $parentgroupid
When you submit the form (which presumably exists since you are referencing it from your JavaScript): Get the value from $_GET['name_of_form_control'] in the document referenced by the action attribute of the form.
When you submit the form, depending on the method specified in the form ("GET" or "POST"), the value will be in either the $_GET or $_POST arrays on the page to which you are submitting. You can retrieve the value by name (the name of the control):
$parentgroupid = $_GET['parentgroup'];
or
$parentgroupid = $_POST['parentgroup'];

Drop down selection after saving

I do have a two drop downs called as source and campaign and these two drop down showing the data that are coming from the data base.i do have others input fields as well.my concern is that i want to save this data after filling it in the given input and selecting drop downs data it must be saved but after saving the data the drop downs must show the selection that i had selected while clicking on save button but it is showing default one.
my code is as follows:
This is for Source:
$result= mysql_query("SELECT * FROM infosources where kunnr = '".$_SESSION["kunnr"]."' order by sort_order asc");
$model["source"]=array();
while($row = mysql_fetch_array($result)){
array_push($model["source"],$row);
}
This is for campaign:
$result= mysql_query("SELECT * FROM s_campaigns WHERE kunnr ='".$_SESSION["kunnr"]."' and active = 'true' order by name asc");
$model["campaign"]=array();
while($row = mysql_fetch_array($result)){
array_push($model["campaign"],$row);
}
and my dropdown is as follows:
<select name="srcid"> <?php foreach($model["source"] as &$obj){?>
<option value=<?php echo $obj["srcid"];?>> <?php echo $obj["srcname"];?> </option>
<?php }?></select>
and the other drop down is
<select name="camp_id"> <?php foreach($model["campaign"] as &$obj){?>
<option <?php if($model["selected"]==$obj[""]){?>selected <?php }?> value=<?php echo $obj["id"];?>> <?php echo $obj["name"];?> </option>
<?php }?></select>
please suggest me on this...
for that you have to decide the value in view at time of refreshing your page
this is a very simplivied example and is just working if you send the form to the same file ... if you send the form to any other file or if you pass the $selectedCampId variable from the other file back to this html form
<?php
// fetch the database to get all possible campaings always... before form was saved and also afterwards
$result= mysql_query("SELECT * FROM s_campaigns WHERE kunnr ='".$_SESSION["kunnr"]."'
and active = 'true' order by name asc");
$model["campaign"]=array();
while($row = mysql_fetch_array($result)){
array_push($model["campaign"],$row);
}
// initiate $selectedCampId .... if the form is sent ... this variable will be filled with the campaign_id so that we know which option was selected... otherwise it wil remain empty..
$selectedCampId= '';
// if the save button was pushed, the form method is POST and the camp_id is not empty
// save the value of the camp_id input field to the variable so that in the next step
// we know which one was selected
if(!empty($_POST['camp_id'])){
// validate the input and save the data to database
// check which campaign id is selected and write it to variable
$selectedCampId= $_POST['camp_id'];
}
?>
<?php // the form method is POST otherwise use $_GET to fetch the camp_id after the form was sent ?>
<form method="post">
<select name="camp_id">
<?php foreach($model["campaign"] as &$obj): ?>
<option
<?php // if form is sent and $selectedCampId is not empty ... echo selected = "selected" ---- otherwise echo empty string ?>
<?php echo ($obj['id'] == $selectedCampId) ? 'selected = "selected"' : ""; ?>>
<?php echo $obj["name"];?>
</option>
<?php endforeach;?>
</select>
</form>

Categories