I am trying to update a session value by dropdown onchange event and use this session value to a second page. For this I have added following dropdown list and a submit button.On submit button press I am updating the session value.It is working sometime but most often it is giving the previous session value even after changing the dropdown list option.Whats wrong with it
<?php
session_start(); // start session before output
if( isset($_POST['submit']) ) { // some other page is posting to this page?
// save values from other page to session
$_SESSION['amount'] = $_POST['select_amount'];
}
?>
<form name="myForm" action="/thanks" method="POST">
<table> <tr> <td style="width:272px;">
<select name="select_amount" id="select_amount" onchange="submit()">
<option value="0">select a listing type</option>
<option value="10">Premium Listings </option>
<option value="20">Premium Blogs </option>
<option value="30">1 week sticky </option>
</select></td>
<td style="text-align:left;color:gray;"><span style="color:crimson;">*
</span>Select a listing type</td></tr> </table>
<table> <tr>
<input type="submit" name="submit" class="button_add" onsubmit="return
validateForm()">
</form>
if you want to change session value using on change then no need to form submit, use ajax then you can do it easily
Step1: create your main file like this
<?php
session_start(); // for show your session value
print_r($_SESSION); // remove this after check
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form name="myForm" action="/thanks" method="POST">
<table>
<tr>
<td style="width:272px;">
<select name="select_amount" id="select_amount" onchange="update_session_value(this.value)">
<option value="0">select a listing type</option>
<option value="10">Premium Listings</option>
<option value="20">Premium Blogs</option>
<option value="30">1 week sticky</option>
</select></td>
<td style="text-align:left;color:gray;">
<span style="color:crimson;">*
</span>Select a listing type
</td>
</tr>
</table>
<input type="submit" name="submit" class="button_add" onsubmit="return validateForm()">
</form>
<script>
function update_session_value(value) {
$.ajax({
type: "POST",
url: 'http://localhost/session.php', // change url as your
data: 'select_amount=' + value,
dataType: 'json',
success: function (data) {
}
});
}
</script>
then create simple session.php file
<?php
session_start();
if( isset($_POST['select_amount']) ) {
// save values from other page to session
$_SESSION['amount'] = $_POST['select_amount'];
}
?>
after on change refresh the page you can see this result
use the ajax call on change event
$("#select_amount").change(function(){
var select_amount = $("#select_amount option:selected").val();
$.post('change_session.php',{select_amount : 'store'},function(data){
if(data != ''){
alert(data);
}
});
});
put a php script on
change_session.php
Related
I need to send a option to a PHP script, but the instead of a user clicking on a submit button, they simply click a type link.
For instance:
<select name="test">
<option value="1">1</option>
</select>
Next
So the user will select 1, then click on the link that says "Next". It's almost like i need to read the select tag BEFORE sending it to the php by a GET request (php.php?select=1). Any ideas how I can pass over this roadblock?
You Have to use <form> tag to make this possible or use ajax submit tu submit your form with <form> you can do in this way
<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use input submit instead-->
<input type="submit" name="submit" value="Next"/>
</form>
Or you can also submit it with <button> :
<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use button instead-->
<button type="submit">Next</button>
</form>
as you mentioned in question you want to get the value of select before submit to php you can use jQuery ajax to do this look below
<form id="my_form" action="">
<select id="my_tag" name="test">
<option value="1">1</option>
</select>
<button type="submit">Next</button>
</form>
Ajax:
$('#my_form').submit(function(e){
if($('#my_tag').val() !== '') {
var select = $('#my_tag').val();
$.ajax({
type: 'Get', // Could be GET/POST
url: 'php.php?select='+select, //targeting url with value from select tag
success: function(data){
alert(data); // alerting the data which php file returned.
}
});
} else {
alert('error'); // if select tag is empty we will show an error.
}
e.preventDefault();
});
I want to put my select value as my form action + string(/index.php)
but it seems that I can't get the value of the select until I click my submit button.
<tr>
<td>Select Project: </td><td><select name="myproject" onchange="">
<option>Exam1</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" id="login" value="Submit"
formaction="<?php $myproject = $_POST['myproject']; echo $myproject."/index.php"; ?>"
formmethod="POST"/>
</td>
</tr>
That is correct. You are mixing server and client side.
After reading your comment I believe you want this (plain JS chosen)
<script>
window.onload=function() {
document.getElementsByName("myproject")[0].onchange=function() {
var path = this.value;
if (path) this.form.action=path+'/index.php';
}
}
</script>
with the HTML now this:
<form method="post" action="">
Select Project: <select name="myproject">
<option value="">Please select</option>
<option value="exam1">Exam1</option>
</select>
<input type="submit">
</form>
To do it on the server, have a look at Post to another page within a PHP script
$myproject = !empty($_POST['myproject']) ? $_POST['myproject'] : "defaultaction.php";
I've got a plugin I'm working on, part of which consists of a 'conditional' dropdown on the options page. Basically, I've got a dropdown with three options, and a second dropdown gets populated with info from the database depending on what is selected in the first dropdown. Problem is, I'm at a loss as to how to populate the second dropdown! I'm not really that great with AJAX, but it seems the likely solution. Any thoughts from the more seasoned devs out here?
EDIT: Here's where I currently am, but it never seems to get run:
<form action="<?php echo site_url() ?>/wp-admin/admin.php?page=ad_manager&pagetype=addedit&pid=<?php echo $_REQUEST['id']; ?>" method="post" name="ad_success">
<table cellpadding="5" class="widefat post fixed">
<thead>
<tr>
<th><strong><?php if($_REQUEST['id'] != '') { _e('Edit Ad'); } else { _e('Create Ad'); } ?></strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label for="gtad_condition">
<strong><?php _e('Ad Condition'); ?>:</strong>
<select name="ad_condition" id="ad_condition">
<option value="">Select an option...</option>
<option value="is_city">Is City</option>
<option value="is_location">Is Location</option>
<option value="is_page">Is Page</option>
</select>
</label>
<label for="gtad_location">
<select name="ad_location" id="ad_location"></select>
</label>
</td>
</tr>
</tbody>
</table>
</form>
<script>
jQuery(document).ready(function() {
$ad_condition = $("select[name='ad_condition']");
$ad_location = $("select[name='ad_location']");
$ad_condition.change(function() {
if ($(this).val() == "Is City") {
$("select[name='ad_location'] option").remove();
$("<option>Test</option>").appendTo($ad_location);
}
});
});
</script>
The reason why your code above does not work, is because $("mySelect").val() returns the value attribute of the <option>, not the text between the beginning and end tag. See my fiddle: http://jsfiddle.net/XhujD/ .
So, you solve the problem by changing
if ($(this).val() == "Is City") {
into
if ($(this).val() == "is_city") {
Using AJAX to populate the second dropdown when the first one has changed will basically look as follows:
// wait until the first list has changed
$("#list1").change(function(){
// call a script using AJAX and pass the selected value
$.post('myScript.php', {myValue: $(this).val()}, function(data){
// clear the second list
$("#list2").find("option").remove();
// add the options based on the 'data' variable
$("#list2").append('<option value="myOption">' + data + '</option>')
});
});
I asked this question previously but i think i made it too complex, I want to assign a query if a value from a list is selected.
<form id="filter" name="filter" method="post" action="">
<select>
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
What i want to achieve is if user selects petrol the run the following query
$query = mysql_query("SELECT fuel_type from car WHERE fuel_type = 'petrol'");
<form id="filter" name="filter" method="post" action="">
<select id="college" name="fuel" onchange="changeValue();">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
If you want to post the value in form submit use like this
$ctext = $_POST['fuel'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
//process $row
If you don't want to submit the form, you can use jquery in onchange event...
js code
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function changeValue() {
var ctext = $("#college option:selected").val();
$.ajax({ url: "yourfile.php",
data: {"ctext":ctext},
type: 'post',
success: function(output) {
//display output here
}
});
}
yourfile.php
<?php
//connect to db
$ctext = $_POST['ctext'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
echo $row['fuel_type'];
?>
Give your select tag a name:
<select name="fuel">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
Then in your PHP you can do something simple like:
<?php
if(isset($_POST['cmdFilter']))
{
echo "You selected the fuel type: " . $_POST['fuel'];
}
?>
You can also set the select to have an id instead and use JQuery to send an ajax request passing it the value by giving selecting it by $("#myIdforSelect").val()
P.S
Be sure to sanitise your input ;)
Although I would also suggest the AJAX method, I just want to make it clear that you do not HAVE to use AJAX to accomplish this.
If your filter options are not too complex or many, then you can simply do a pull for all available options for any level of the filter tree on load and hide them initially. Then, as the user drills down into each section, you show them the next one accordingly (via javscript). In the end you can just submit a regular form with all the selected form fields via GET or POST.
This can effectively save bandwidth by running less calls to the server also :)
BUT if your choice tree gets too complicated, it might be better to just run AJAX request on user interaction.
First Select
<Select id="drop1" onchange="drop1(this.value)">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>
<div id = "txtHint1"></div>
Once a value has been selected, the value will be sent to ajax. And corresponding dropdown will be generated on another page and show in div txthint1.
Second Select
<Select id="drop2" onchange="drop2(this.value)">
<option value ='111'>111</option
<option value ='222'>222</option
<option value ='333'>333</option
</select>
<div id = "txtHint2"></div>
As the generated dropdown is another php page, how can I get what the value the user has selected to show on my current page (div txthint2)?
I will be working in php.
$value = $_GET['a'];
There are no submitted button.
Send out the second request on a successful response:
function one() {
$.ajax({
...
success: function() {
two(TheValueFromOne);
}
});
}
function two() {
$.ajax({
...
});
}
What is happening is that once the request has been made successfully, then it is automatically starting the next request right after.
However, if you are jsut passing values around form one page to another you really do not need AJAX.
FOR EXAMPLE:
test1.php:
<form action="test2.php" method="get">
Book Title <br />
<input type="text" name="booktitle" /><br />
<input type="submit" name="submit" value=" - Update Database - " />
</form>
test2.php
<body>
<? echo $_GET["booktitle"]; ?>
</body>
IN YOUR CASE:
test1.php:
<Select id="drop1" name="test1">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>
test2.php
<Select id="drop1">
<option selected = "<? echo $_POST['test1']; ?>"></option>
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>
#QUESTION:
<div id = "txtHint2"><? echo $_POST['test1']; ?></div>
#POSTING WITH FORM:
<form method="post" action="test2.php" >
<Select id="drop1" name="test1" onChange="this.parentNode.submit()">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>
</form>