PHP dropdown sticky - php

I have a database populated drop down list.
$options4="";
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$options4.="<OPTION VALUE=\"$id, $thing\">".$thing;
}
?>
<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT NAME="ClientNamefour" OnChange="this.form.submit()">
<OPTION VALUE=0>Client
<?php echo $options4?>
</SELECT>
</FORM>
Once this changes it pulls a client name from a database and brings up some information there is a form that can be filled out to insert more information into a database. Once that form is filled out this page sends the information to another page with the code to insert the data into the database. Then I am using a header at the end of that code to send it back to this page. But when it comes back to this page I want it to comeback to the client they selected before and not a blank screen. So how do I make a DB populated list automatically comeback to the previous selection using php?

All you need to do is modify the way you construct the options to spot when you are creating the selected one, then add a SELECTED attribute to it...
//obtain current value from GET, POST or COOKIE value...
$current=isset($_REQUEST['ClientNamefour'])?$_REQUEST['ClientNamefour']:'';
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$value="$id, $thing";
//figure out if we should select this option...
$sel=($value==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">".$thing;
}
One way to keep the value persistent would be to store it in the session, e.g. when you process the form
//assuming session_start() has been called...
if (isset($_POST['ClientNamefour']))
$_SESSION['ClientNamefour']=$_POST['ClientNamefour'];

Related

How to convert session variable into POST

ok so i have two selection boxes, upon me pressing submit on the form it takes the POST data and creates a session variable for each of them. These session variables are then used to fill the selection box with what the user has selected. However, i need to reuse the data as POST after it is submitted, but its my html is saying the field is blank even though it is displaying the session variable.
So im thinking the actual selection box is empty when filled with the session, even though i can see it?
I need to manipulate this data later on, so how can i make my php recognize it as POST after my form has been submitted.
if any more code is needed please let me know
<select name="team-1" required>
<option value='Holder' disabled selected>
<?php
if(isset($_SESSION['t_team1'])){ //echoes currently set team
echo $_SESSION['t_team1'];
}else {
echo "Team 1";
}
?>
</option> <!--Placeholder for Select-->
<?php
while($rows = $teamSet->fetch_assoc()){ //Fills select with teams matching cup
$teamName = $rows['team_name'];
echo " <option value='$teamName'>$teamName</option>";
}
?> <?php mysqli_data_seek($teamSet, 0); ?> <!--RESETS WHILE LOOP, DUPLICATE AFTER EVERY TEAM "WET"-->
</select>

Extract value from select/option tag in php?

I am not exactly how to do it and how to word the question, so i shall try my best (PS: i'm new to web dev, so please be clear in your answers if you could).
So, I have got a drop down menu with the list names, which are taken from my database. In that database i have a table with names column (the ones that are rendered to the dropdown box) and relevant information to those names. Now, I want that relevant information to appear below in a tag when a user choose one of those names. I also cannot use the form.submit() method because my submit button is already taken for something else.
Here is the code to that bit:
<form name="name_choice" method="post" action="index.php">
<select name="names" onchange="form.some_method()">
<option value="NULL" selected="selected">--Select name--</option>
<?php
for ( $i = 0; $i < $numrows; $i++ ) { //for all the columns, iterate and print out
$id_names = mysql_result($result, $i);
echo "<option value='".$id_names."'>".$id_names."</option>";
}
?>
</select>
</form>
So the bit above works fine, but the "some_method()" is my problem, i don't know what to trigger to display the text in the div below the drop down box (code is below for it):
<div class="information"> <!--if the name is chosen ONLY!-->
<?php
if($_POST['names'] == "NULL") {
echo '<p>Please select an option from the select box.</p>'; //this bit is for testing
}
else {
echo '<p>You have selected: <strong>', $_POST['names'], '</strong>.</p>';
//and then how to echo the relevant information?:(
}
?>
</div><!--end of possible info-->
onchange is a JavaScript event. PHP can't do realtime processing of form data, as it sits on the server and the form is on the client. You can sort of do it by using AJAX and passing the form data as the user types, but that would be a lot more work than is needed. Take a look at JavaScript form validation posts to get yourself headed on the correct path.

Select dropdown value to get a populated data from database mysql

I have a populated dropdown list from mysql on one page.
What code should i write in php so that on selection of dropdown list's value, a new page is opened and data is fetched from mysql, provided i have required fields in database.
<form name="form1">
<select>
<?php
$con=mysql_connect("localhost","FreeUser","123456");
if(!$con)
{
die('Connection failed' . mysql_error());
}
mysql_select_db("human_resource", $con);
$sql1=mysql_query("SELECT eid_emp,name_emp FROM employee_details");
while ($data=mysql_fetch_assoc($sql1))
{
?>
<option name="drop1" checked value ="<?php echo $data['eid_emp'] ?>" >
<?php echo $data['name_emp']; ?>
</option>
<?php
}
mysql_close($con);
?>
</select>
</form>
The answer is there is no PHP code to do what you're asking.
You'll need two things: 1). Some javascript to hook into your form and 2). a way to render the resulting query.
here is some javascript to hook into the form, assuming jquery for brevity (although you should just give the select an ID or a class, which would make the selector less obnoxious):
$('form[name="form1"] select').change(function(){
location.href='renderer.php?dataKey=' + $(this).val();
});
From there, you'll navigate to renderer.php along with $_GET value for dataKey. render it as your will. If you want to open a new window, use a window.open call instead of setting location.href.
has nothing to do with php or mysql ... you could add an "onchange" handler to your < select > element ...
<select onchange="javascript:someFunctionCallHere()">
a call to your forms submit() method should be what you want...
Not a lot of information, but you could do something like this:
<form name="form1" method="POST" action="page.php">
<select onchange="form1.submit()">
Then in the head of your page
<?php
if(count($_POST))
{
// do stuff
header( 'Location: http://somesite.com' ) ;

implement a button to send information to another php file?

I've got the following php code printing out the contents of a SQL table.
$query="select * from TABLE";
$rt=mysql_query($query);
echo mysql_error();
mysql_close();
?>
<i>Name, Message, Type, Lat, Lng, File </i><br/><br/>
<?php
while($nt=mysql_fetch_array($rt)){
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file]";
}
}
?>
How would I implement a button so for each "row" if the button is clicked on that row it'll submit the information of that row to another php file?
I want it looking something like...
details details2 details3 BUTTON
details4 details5 details6 BUTTON
details7 details8 details9 BUTTON
details10 details11 details12 BUTTON
Where if BUTTON was hit on row 1 details1,2,3 would be sent to a php file, on row 2 detals 4,5,6 would be sent etc. How would I do this?
Thanks.
it's going to be something like that, depending on the data you need to send:
while($nt = mysql_fetch_array($rt)) {
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file] ".'send request<br/>';
}
}
You can either use GET method and send a query string to the second php page and receive the variables there, like
next.php?variable1=value1&variable2=value2&...
or use POST method by making a hidden form for each row and assign a hidden field for each variable you want to send.
<form method="post" action"next.php">
<input type="hidden" name="variable1" value="value1" />
<input type="hidden" name="variable2" value="value2" />
.
.
.
</form>
or instead of sending all the values, just send the row ID (if any) using any of these two methods and run another query in next.php to get the information you need from database.
Instead of submitting the entire data, just send the ID and fetch the results from the database in the other script. If you want to have an input button, you can do
<form action="/other-script.php" method="GET">
<?php printf('<input type="submit" name="id" value="%s" />', $nt["id"]); ?>
</form>
but you could also just add a link, e.g.
printf('Submit ID', $nt["id"]);
If you really want to send the entire row values over again, you have to make them into form inputs. In that case, I'd send them via POST though.

PHP drop down and Session question

What really needs to happen here is.. A session takes my data from the drop down listed below. Its gonna take it to the process page then bring it back to this page with a header on the process page. Then when it gets back to the first page the dropdown will populate with the session or the same client as when I left that page and the on change of the drop down will work. Can anyone help me with this. Thank you Right now this returns the client to the drop down but the onchange for the drop down does not work which is what I really need to work.
session_start();
$current = isset($_SESSION['ClientNamefour']) ? $_SESSION['ClientNamefour'] : 0;
while ($row = mysql_fetch_array($result)) {
$id = $row["Client_Code"];
$thing = $row["Client_Full_Name"];
$value = "$id, $thing";
$sel=($id==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">".$thing;
}
?>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="ClientNamefour" onchange="this.form.submit()">
<option value=0>Client
<?php echo $options4?>
</select>
</form>
Process page header
session_start();
$_SESSION['ClientNamefour'] = $_POST['txtclientcode'];
// Do the redirect
// Do the redirect
header("Location: test.php");
exit();
I don't know your question or problem but I just guess that it should be
$_SESSION['ClientNamefour'] = $_POST['ClientNamefour'];
instead of
$_SESSION['ClientNamefour'] = $_POST['txtclientcode'];
Also don't forget to put closing </option> tags into your code.
Of course you have to send the the form data to the process page, but
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
looks like the data gets send back to the same page.
And never trust data submitted by the user.
Edit:
Try:
onChange="Javascript:document.forms[0].submit()"
or
onChange="Javascript:document.forms['form'].submit()"
But that is only what I found using Google.

Categories