How to convert session variable into POST - php

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>

Related

PHP How do I get my form inputs to have data that is auto generated based on selection from database

This is a bit hard for me to explain and I don't really have any examples to show as I don't know how to do it.
What I am trying to do is to have fields automatically filled up for me based on my selection which is taken from my database.
Let's say I'm trying to fill data up for a casualties form and I'm getting my selection for my fields from a people's database and that database contains all the information of everybody inside, information such as the contact number, next of kin, full name, their IC etc etc.
Is there a way to display data such contact number, next of kin, IC number based on let's say their name inside the form itself?
$query = "SELECT * FROM staff";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
.... in the form itself
<tr>
<td><label><b>Staff ID</b></label></td>
<td><select name="staffID"><option value="">Select staff ID</option>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<option value="<?php echo $row['staff_id']; ?>">
<?php echo $row['staff_id']; ?>
</option>
<?php } ?>
</select></td>
</tr>
The idea is, after they selected an ID from the dropdown box, additional fields will appear, giving the user more details of the staff such as their full name, contact number etc etc.
I can echo out all their ID and I know how to generate out additional fields but I don't know how to auto fill up data on those fields based on the staff_ID they selected.
Sorry if this is confusing, english is not my main language and I'm pretty terrible with programming
English is not my natural language (i`m from brazil).
In this case you will need to use AJAX. The easier way to do that is using Jquery (www.jquery.com). I will put here the easier way, but if cant help, tell me again.
---Let`s go --------------
<option id="reportedBy" name="reportedBy">
<option value="staff" data-name="Full Name of Staff" data-no="(222)2222-2222">
</option>
So you will make a script using jquery. When this option change his value, you will get the selected option value and get the others data using the dataset.
<script>
$(document).ready(function(){
$("#reportedBy").change(function(){
name = $(this).find(":selected").data("name"); //data-name less data-
no = $(this).find(":selected").data("no"); //data-no less data-
//Assuming your textfilds use the id "name" and no
$("#name").val(name);
$("#no").val(no);
});
});
</script>

Remembering php form data to display currency all over a website

I've set up a currency conversion dropdown in a wordpress site.
The only thing missing is that every time I load another page, the currency will reset as the form selection was 'forgotten'.
Any ideas how to do this? I tried a suggested js cookie that I saw here, but it doesn't work.
This is what I got so far:
<form name="myform" id ="myform" method="post">
<select name="currency-select" id="sort" onchange="submitform();">
<option value="" selected="selected">Currency</option>
<option value="0">U.S Dollars (USD)</option>
<option value="1">Euros (EUR)</option>
<option value="2">British Pounds (GBP)</option> `
</select>
</form>
js:
function submitform()
{
document.myform.submit();
}
I tried using this code as recommended here but it doesn't really work out for me, I think I didn't do it the right way -
<?php
`session_start();`
if (isset($_POST['currency-select'])) {
$_SESSION['sort'] = $_POST['sort'];
}
?>
I added the $_SESSION to the form as well:
<option value="0" <?php if($_SESSION['sort'] == "0") echo "selected";?>>U.S Dollars (USD)</option>
UPDATE
I've made a few tests. The session seems to be saved (as I echoed it on a few pages while refreshing etc.) I guess the only problem now is related to the form itself. Even with the right session number, I can't get it to select the right option.
I've tried two methods, but both does not work:
<option value="0" <?php if($_SESSION['currency-select'] == "0") echo 'selected="selected"';?>>U.S Dollars (USD)</option>
or
<option value="0" <?php if($_SESSION['currency-select'] == "0") echo "selected";?>>U.S Dollars (USD)</option>
I'd store the selected value in a $_SESSION['selected_currency'] variable and the cross check and select it when the drop down is being populated with the currency list.
Assuming that the sessions are working, I will use something like below to keep the currency selected in your drop down.
<select name="currency">
<?php
foreach($currency as $value){
if($value->currency_code == $_SESSION['currency']){
echo "<option value='$value->currency_code' selected='selected'>$value->currency_name</option>";
} else {
echo "<option value='$value->currency_code'>$value->currency_name</option>";
}
}
?>
</select>
There could be shorter ways, I am using this for illustration purposes.
For permanent retain of data you only have a few possibilities, the easiest to implement are $_SESSION, $_COOKIE or in a Database.
You have two options to do that
1st is by adding a field to the options.php page and save your data then get back your data from the options.php for that you've to use update_option('nameOfField_form','nameOfFieldDb'); and get_option('nameOfFieldDb').
and 2nd option is by jquery.ajax(); method save your data in options.php
you may find these links helpful codex
get_option
update_option

mysql update query with form array

Multiple posts but I'm still stuck...I'm missing something fundamental here. I have a form with a select:
<select name="camera_status[]">
<option <?php echo $enabled_option; ?>>Enabled</option>
<option <?php echo $disabled_option; ?>>Disabled</option>
</select>
This form is built with a loop to give a list of all camera settings. So you would have multiple cameras and their corresponding camera_status. Also I have a hidden input field with the camera_id:
The camera_id is processed with some javascript. Then I process that with:
$camera_id = $_POST['camera_id'];
if (is_array($_POST['camera_status']))
{
foreach ($_POST['camera_status'] as $camera_status) {
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}
If I echo the camera_id it is correct. But my foreach runs an update query for the full list of cameras instead of just the one selected. So it updates only the last camera in the list. Let me know if it makes sense to update the full code. Obviously I'm going about this all wrong...
EDIT: Well if you have single selection then it is simpler than that:
HTML:
<select name="camera_status">
<option value="Enabled">Enabled</option>
<option value="Disabled">Disabled</option>
</select>
And PHP:
$camera_id = (int) $_POST['camera_id']; //Here you had SQL injection.
$camera_status = mysql_real_escape_string($_POST['camera_status']); //Neither that was protected.
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
You have a fundamental flaw in your thought process for the page. When you output more than one <select name="camera_status[]"> elements, you're going to get that many results back. With two of them, you'll get two values in the array, and so on.
What it sounds like you're doing is outputting a list of cameras, having the user select a camera to modify, and then, from then on, all of the camera settings now only apply to that one specific camera. If this is the case, then you don't need to use arrays for the camera settings, including camera_status. Just remove the array portion and stop outputting more than one HTML element for each camera setting (since you know that once a camera is selected, those values apply to that specific camera).
However, if your page that displays the multiple cameras allows the user to modify every camera and its settings, you'll need to accommodate for the user's input.
If the latter is the case, here's a neat trick - Modify your <select> so it looks like this when you're outputting your camera form:
<select name="camera_status[<?php echo $row['camera_id']; ?>]">
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</select>
Now, when you grab $_POST['camera_status'], it'll be an array with the camera IDs as the keys and their selected value as the value. So now, you can do this:
if( is_array($_POST['camera_status']))
{
foreach ($_POST['camera_status'] as $camera_id => $camera_status) {
$camera_status = intval( $camera_status); // Be wary of SQL injection
$camera_id = intval( $camera_id);
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}
Now this will update every camera with the correct value chosen.
Easy enough. Add value attributes to the <option> tags so that each of them will have a value
<select name="camera_status">
<option value="1" <?php echo $enabled_option; ?>>Enabled</option>
<option value="0" <?php echo $disabled_option; ?>>Disabled</option>
</select>
Then, in your php, look for that value
foreach ($_POST['camera_status'] as $camera_status) {
if($camera_status == 1) {
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}

PHP dropdown sticky

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'];

'Remember' form drop list value when submitting to SELF?

I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared).
I have solved this in regular fields and checkboxes like this:
<input type="text" name="headline" id="headline" value="<?php echo #$_POST['headline'];?>">
But how can I do this with drop lists? or radio buttons? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. Didn't work!
So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ?
If you need more input let me know, thanks!
For selects, you need to compare each option to your posted value, and handle it individually. Simply print out your options in a loop, and test each value against the value was was previously posted. If it maches, add selected to the attributes of that particular option.
$color = $_POST["colors"];
$colors = array("red","green","blue");
<select name="colors">
<?php foreach ($colors as $option) { ?>
<option<?php print ($option == $color) ? " selected" : ""; ?>>
<?php print $option; ?>
</option>
<?php } ?>
</select>
Actually, found out that it is possible to set the selectedIndex with javascript...
So I could put the selectedIndex in a hidden input before submitting the form, and then get that selectedIndex and set it with a javascript function... tricky but suits me better in this case...
document.getElementById("select").selectedIndex=nr;
Thanks though Jonathan!

Categories