In my form action there's a url: www.url.com/?quantity=$quantity
And in the form there's a select box where customers choose the quantity.
<form method="post" name="jform" action="www.url.com/?quantity=$quantity">
<select class="font_12" id="quantity" name="quantity">
<option value="10">10 PCs</option>
<option value="25">25 PCs</option>
<option value="50">50 PCs</option>
<option value="99">99 PCs</option>
</select>
I am trying to get the value in the select box using ajax, and then display into the action form url. I did a alert and it works, I am getting the value of the select box.
But I don't know how to put this vaue into the PHP varaible $quantity?
Here's my Ajax code:
$('#quantity').on('change', function() {
var val = $(this).val();
if(val != '') {
$.get('index.php', {'quantity' : val}, function(resp) {
alert(val);
});
}
});
Actually I want it to change the php variable right away when the quantity in the select box change before submitting the form.
Any help?
Use $_GET
If your URL is ?quantity=### then just use $_GET['quantity'] in your PHP code.
To change the action attribute on the form when you change the quantity you can just put the following inside your onchange event:
$('form[name="jform"]').attr('action','http://url.com/?quantity=' + val);
Related
my selectbox:
<div>
<select name="JnsDaftar" id="JnsDaftar" required>
<option value="" disabled selected></option>
<option value="A">1 - A</option>
<option value="www.google.com">2 - google.com</option>
<option value="B">3 - B</option>
<option value="C">4 - C</option>
<option value="D">5 - D</option>
</select>
<label for="JnsDaftar">KARABO TANGKAL!</label>
</div>
N i want to redirect to google.com if I select option 2. Other Value not redirect.
Thx.
-- Edit --
I want to live redirect it to new page if I select option 2. Not via Post request. maybe javascript or Jquery. And the other option not redirect (the other option i want to save value by post request). thx
-- Edit --
Sample: JSFIDDLE
if php you can get this post request
if($_POST['JnsDaftar'] == 'www.google.com') {
header('Location:www.google.com');die;
}
if js you can get this value look comment Fast Snail and this Get selected value in dropdown list using JavaScript? link for get how to and adding just if
<script>
// code for get value
if($value == 'www.google.com') {
window.location.href = 'www.google.com'
}
</script>
I Solved with this code:
$( document ).ready(function() {
$('#JnsDaftar').on("change", function(e){
if($(this).val() == 'www.google.com'){
swal({
text: "Anda akan diarahkan pada laman PERBAIKAN BIODATA WNI!",
type: "info",
confirmButtonText: "OK"
}).then(
function(isConfirm){
window.location.href = "ihttp://www.google.com";
});
}
//window.location.url="www.google.com";
});
});
I made an editable dropdown menu :
<html>
<body>
<div style="position:relative;width:200px;height:25px;border:0;padding:0;margin:0;">
<select style="position:absolute;top:0px;left:0px;width:200px; height:25px;line-height:20px;margin:0;padding:0;" onchange="document.getElementById('displayValue').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
<option></option>
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
<input type="text" name="displayValue" placeholder="add/select a value" id="displayValue" style="position:absolute;top:0px;left:0px;width:183px;width:180px\9;#width:180px;height:23px; height:21px\9;#height:18px;border:1px solid #556;" onfocus="this.select()">
<input type="hidden" name="idValue" id="idValue">
</div>
</body>
</html>
I want to POST the value that was added, so as to include it in drop down for the next round.
I suggest you use jQuery for easy implementation. You can wrap them in a form and do the POSTing via jQuery Ajax then store that value somewhere for future use then append it to the next as the new option item.
POST via aJax
$(function() {
$('#form').on('submit', function(e) {
// do not reload the page
e.preventDefault();
// do the posting
var newValue = $('#displayValue').val();
$.post('http://MYURL.com/post.php', {
new_field: newValue
}, function(data) {
// success callback
$('#form > select').append('<option value="'+newValue+'">'+newValue+'</option>');
})
});
})
Basically, you post newValue to http://MYURL.com/post.php and handle that new data then the success callback will handle the inserting of the new value to your select.
the code is not tested, let me know if it did not work
Learn more about jquery.post() here and more about jquery here
I retrieve the value of a select box in this way which goes to populate a input field:
<script>
$(document).ready( function ()
{
$('#dropdown_selector').change(function()
{
var option = $(this).find('option:selected').val();
$('#showoption').val(option);
});
});
</script>
<select id="dropdown_selector">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
</select>
<label>Value</label>
<input type="text" name="name" id="showoption" disabled="disabled" />
</label>
But my problem is how to retrieve that value and use it directly in a PHP variable,instead of populating the input field(with id showoption).
Something similar as:
$variable=(var selected in select box)
Thanks for your help!!!
This jQuery code is triggered when an option is selected and then stores the value of the option into variable valu. After that it sends the variable to your php document via Ajax post.
$('select').change(function(){
var valu=$( "select option:selected" ).attr('value');
$.ajax({
type:"POST",
url:"yourphpscript.php",
data: {values:valu}
});
});
http://jsfiddle.net/joey6978/peq74/ :this fiddle shows the select choosing trigger.
In your php script you should have the post method with the name values to recieve your data.
$myvariable=$_POST['values'];
I need to pass select value to a session without refresh the page.
I think onchange event can be used.
Actually, I can put select value into text field.
HTML
Select:
<select id="dropdown1" class="select" tabindex="2" onchange="run(this)">
<option>Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
TextBox:
<input type="text" id="car" name="car" id="car" value="<?=$_POST[car]?>" disabled />
JavaScript
function run(sel) {
var i = sel.selectedIndex;
if (i != -1) {
document.getElementById("car").value = sel.options[i].text;
}
}
Question
How to set value on a variable when select value from dropdown?
$_SESSION['car'] = DROPDOWN VALUE
if you can use jQuery, this will work:
function run(sel) {
var i = sel.selectedIndex;
if (i != -1) {
document.getElementById("car").value = sel.options[i].text;
$.ajax({
type: "POST",
url: "urPHPPage.php",
data: { car: sel.options[i].text}
}).done(function( msg ) {});
}
}
and in your PHP file
if(isset($_POST["car"])){
$_SESSION['car'] = $_POST["car"]; //offcourse you will have to do some sanitization here
}
Since your session is a server-side event and your onchange event is a client-side event, you'll need to use AJAX in order to change your dropdown options without refreshing the page.
You will need to run a JavaScript function on onchange event.
In that function you need to use AJAX in order to connect to your browser/database and save the session['car']
Hope it helps.
I have this script and I want to get the value of drop down select list in the PHP and check the if (jws1 != "") then show 1 to 10 value in the second drop down box using of for loop...
JavaScript code:
<script language="javascript" type="text/javascript">
function test()
{
var e = document.getElementById("JWSections");
var Sections = e.options[e.selectedIndex].value;
alert(Sections);
}
</script>
HTML code:
<select name="JWSections" id="JWSections" onchange="test();">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
I want to get the value like jws1, and check in the PHP.
For PHP to be able to do anything with your HTML form data, you'll have to submit it to PHP.
You can either submit the form itself or use AJAX to pass in the value of jws1 and build the options from the result. Jquery is pretty good with doing that kind of thing, all you gotta do is set your AJAX request's data type to HTML and assign the data from the "success" callback to your destination drop down list.
<select name="JWSections" id="JWSections" onchange="test(this);">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
<select name="JWSections" id="secondDropDown"> </select>
<script language="javascript" type="text/javascript">
function test(dropdown)
{
var selectedjws = dropdown.options[dropdown.selectedIndex].value;
var secondDropDown = document.getElementById('secondDropDown');
$.post('path', { selectedjws : selectedjws }, function(key, value){
// Remove all options from second dropdown
for(var i = 0; i < secondDropDown.options.length; i++){
secondDropDown.remove(i);
}
// Add options by key value pair returned from server
secondDropDown.add(new Option(value, key))
});
}
</script>
jsfiddle: http://jsfiddle.net/KTq6y/1/