I have a form and table like this.
What I'm trying to do is - If the select box is selected with a value, it is stored in session on submit(post) and i would like to retain the session value in select box.
If there is session value, it will display the session value in select box, else it will display all.
The below case is not working in my case.
What the below code do is - first time it doesn't set the session value. If I submit again(second time) the session is stored with the first value(first submit value) and it goes on like this.
Hope this question is answerable.
EDITED:
<?php
session_start();
if($_POST){
$_SESSION['book_id'] = $_POST['book_id'];
?>
<table class="table table-bordered" cellpadding="0" cellspacing="0" border="0">
My Table Content
</table>
<?php } ?>
<form method="post">
<?php print_r($_SESSION); ?>
<select name="book_id" class="form-control">
<option value="0">Select Book</option>
<?php while($row=mysql_fetch_array($book_query)){?>
<option <?php if(isset($_SESSION['book_id']) && $row['book_id'] == $_SESSION['book_id']) echo 'selected="selected"'; ?> value="<?php echo $row['book_id'];?>"><?php echo $row['book_name']; ?></option>
<?php }?>
</select>
<input type="submit" value="submit">
</form>
EDIT:
I have print_r for the session value.
What happens is - When I submit the form first time, the session value is empty.
When I submit it second time, the first submit value is stored in session, and it goes on.
Thanks,
Kimz
A few notes:
It seems you are mixing up book_id and book_name.
Also, you don't seem to submit a "book_name" in your form.
The "if($_POST)"-block is executed when the page loads, and not when the form is sent. So essentially, you first try to select something by a value in your session, and then afterwards store said value into your session.
First of all make sure you have session_start() included somewhere.
Secondly, try changing your IF on the options to this:
<?php if(isset($_SESSION['book_id']) && $row['book_id'] == $_SESSION['book_id']) echo 'selected="selected"'; ?>
Notice the isset. Without that on page load you are likely to get a PHP undefined index error.
Finally, try placing the 'if ($_POST) {}' before the form is displayed, so the session gets set before the form is displayed. Currently you wont see any changes until you refresh the page again, this is because the form is being rendered and then the session is set from the POST data afterwards.
Related
I have a problem in accessing session variables outside the code for submit button.when i am printing the session variable within submit code it is printing but at the time of echoing outside submit code it is not printing the value of date .Actually i want to insert the value of session variable in database but it is not getting inserted .the code is given below:
<!Doctype html>
<?php
session_start();
$_SESSION['date']='';
include 'connect.php';
?>
<form>
Date: <input type='date' name='date'> <br>
<input type='submit' name='submit'>
</form>
<?php
$date='';
if(isset($_POST['submit'] ))
{
$date=$_POST['date'];
$_SESSION['date']=$date;
echo $_SESSION['date'];
}
?>
<?php
echo $_SESSION['date'];
?>
It will not echo because you haven't set any form method and getting the values by $_POST.
By default it will take GET as form method.
So you can do two things
Set the form method to POST like this <form method="post"> or
Use $_GET instead of $_POST like this $_GET['submit'] and $_GET['date'].
The form doesn't have a method.
Set form method to POST
So I have a form which has a drop down list, when the submit button is pressed the script task4.php is called. My problem is that I can use the whole option that is selected, however I only need the tid. How do I get just the tid and use it task4.php?
<form action="task4.php" method="get">
<select>
<?php
foreach($results as $row) {
echo "<option>".$row[tid].", ".$row[category].", ".$row[division].", ".$row[clubID].", ".$row[name]."</option>";
}
?>
</select>
<input type ="submit" value="Submit">
</form>
This is what I've got in task 4 and it isn't working:
if (isset($_GET['tid'])) {
$tid = $_GET['tid'];
}
It's possible (perhaps even necessary) that in the absence of a value the browser is sending the text contents of the selected option with the form data.
Just give the option a value:
echo "<option value=\"".$row[tid]."\">" ...
Additionally, that select should really have a name (I don't even see how it could have worked without one):
<select name="tid">
I would like to know how to retrieve a selected value from a dynamically selection box. If I get the selected value then I will store it into another variable that is located in another php file. This variable will help me in a sql query in postgresql.
//First php file
<form name="boton_alta_soniador" action="dar_alta_soniador.php" method="POST" autocomplete="off">
<div class="text-field">
Nombre de la asociacion
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxx password=xxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);
while($results [] =pg_fetch_object($result));
array_pop($results);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php foreach ( $results as $option ) : ?>
<option value="<?php echo $option->i_clave_asociacion; ?>"><?php echo $option->n_asociacion; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
This is just the dynamically selection box. Then I want to store the selected value in this variable:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
So I can query the following statement:
$conocerIDasociacion = "SELECT N_ASOCIACION FROM ASOCIACION WHERE I_CLAVE_ASOCIACION='$ingresaAsociacion'";
I didn't want to use jQuery because the whole system is almost entirely made in PHP and HTML.
Please, any help is welcome and I'm all ears to everyone.
Cheers!
Looks like you're missing a Submit button.
You do not have to use AJAX at all, nor jQuery. You can submit your form and process the selection value as you see fit.
The selected value in the select Tag will be sent to dar_alta_soniador.php, and that file will process that data, using the exact code you wrote:
$_POST['asociacion_seleccion']
So, in dar_alta_soniador.php you will write that code:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
And then perform the query. You do not even have to worry about sending the data around in a session variable, POST does it for you already.
So everything should be OK in your code, or I may have misunderstood your question. Do you have an error message or get some inappropriate behavior?
Maybe the submit button is missing? I use a code like this:
For the select tag:
<div class="controls">
<select name="list_name">
<option>List</option>
<?php
foreach ($nucleos as $inner_array) {
$out = "<option>" . $inner_array['name'] . "</option>";
echo $out;
}
?>
</select>
</div>
And for the Submit button:
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Confirm</button>
<br/>
</div>
</div>
</form>
I am using bootstrap here for style, HTML and CSS. Nothing more.
Best wishes,
I found another solution:
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxxx password=xxxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php
while($row=pg_fetch_assoc($result))
{echo "<option>{$row['n_asociacion']}</option>";}?>
</select>
The point is, whenever the user selects an option from the dyamically selection box, the value of the selection box is known if we call the value with pg_escape_string($_POST['NAME OF YOUR SELECTION BOX']);.
Thanks to all for your collaboration,my problem was resolved.
I am building a site on wordpress and I have encountered a problem that I am not quite sure how to solve.
The Situation:
User makes a selection from a <select> tag on one page and submits their choice
User arrives at a page featuring the same <select>. When the user selects an option, it displays content corresponding with that option using a little bit of jQuery.
-
$('.result').hide();
$('#servicearea').change(function() {
$('.result').hide();
var optionValue = $ (this).attr('value');
$('#'+optionValue).show('fast');
});
The Problem:
I need the selection from the first page to carry over to the second page then run this script.
Solutions:
I'll be honest, I don't know where to start with this, maybe cookies? I was hoping there was a jQuery type of solution as I am sort of comfortable with jQuery.
Any suggestions would be appreciated.
javascript doesnt know what you have posted to the page.
you can put the value into the javascript from php with something like this:
<script>
doSomething(<?=$_POST['selected'?>);
</script>
or you can put it into a hidden form field
<input type="hidden" id="hiddenField" value="<?=$_POST['selected']?>">
and catch the value with
$('#hiddenField').val()
Hidden form fields will work. But if you decide to go with PHP sessions instead, take a look here.
From the above link...
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or maybe pass along the session id, if needed
echo '<br />page 2';
?>
After viewing page1.php, the second page page2.php will contain the session data. Read the session reference for information on propagating session ids as it, for example, explains what the constant SID is all about.
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br />page 1';
?>
UPDATE
Judging by your snippet, I believe you would set the session variable by doing something like this:
<form action="" method="post">
<select type="select" name="servicearea" >
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['optionValue'] = $_POST['servicearea'];
}
echo $_SESSION['optionValue'];
?>
Let's say I have a HTML form:
USA
CDN
And I select option 1 (USA)
Then a php page
Ok, duh, works fine and echo's "1"
How can I display "USA" as well? So in essence, I want to pass along the option's TEXT also. How could I do this?
You would either need to change the option value on the HTML page to be the text you want displayed (poses a XSS security hazard) or on the page that's displaying the text, you'd need an array where all the values match with the forum input.
Ex:
$names[1] = "USA"; $names[2] = "CDN";
Then when you want to display it, you'd call $names[$selection] to output the text.
Another option you can consider is using javascript to update a hidden HTML element on the submitting page when the user makes their selection (using onUpdate). This information would be passed along with the submitted form data. Not the most secure or reliable of options, but an option nonetheless.
The client's browser will not post back anything beyond the form element's name and value - you would have to incorporate additional form fields and Javascript (or change the element's value) to post "USA".
I suggest having a copy of the same data structure you used to print the form.
For example, if you were to do it all in one PHP page...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $key=>$country){?>
<option value='<?php echo $key;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=(int)$_POST['country'];
if(!isset($countries[$chosen])){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $countries[$chosen];?></div>
<?php }
Another option is to ditch the numeric value altogether, and just use the country name as the value of the option. However, for verification, you'll still want the list of values. Check the differences in this example...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $country){?>
<option value='<?php echo $country;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=preg_replace('/[^\w]/','',$_POST['country']);//this isn't strictly necessary, since the next line checks if the value is in your original array - but filtering input is a good habit!
if(!in_array($_POST['country'],$countries)){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $chosen;?></div>
<?php }