I want to "connect" a select with a input text without refresh the page
This is my example
<select name="name_select" id="my_select">
<option value="1">first</option>
<option value="2">first</option>
<option value="3">first</option>
</select>
<?php ...query(SELECT name FROM table WHERE id = value) ?>
Then in var $name there is value from db
<input type="text" value="<?php echo $name; ?>" />
I think I must use jquery post/get but how to update only var that I need to check db?
Like Thamizhan say in comment, you need to use AJAX.
Here is a simple example :
HTML (index.html)
<select name="name_select" id="my_select">
<option value="1">first</option>
<option value="2">first</option>
<option value="3">first</option>
</select>
JS
$('#my_select').on('change', function () {
var selectData = $(this).val();
$.ajax({
type: "POST",
url: "custom.php",
data: {'selectData': selectData },
success: function (json) {
// do things
}
});
}
PHP (custom.php)
if (isset($_POST['selectData'])) {
var_dump($_POST['selectData']); // $_POST['selectData'] is the selected value
// query here
// and you can return the result if you want to do some things cool ;)
}
$("#my_select").on("change", function () {
$.ajax({
url: "phpfile.php",
method: 'POST',
data: {val: $(this).val()},
success: function (data) {
$("[type='text']").val(data);
}
});
});
In phpfile.php
include("db.php");
// ... your code ...
// execute sql(SELECT name FROM table WHERE id = value) and return value.
Please clear one thing why you need sql query here.
<option value="1">first</option>
first is name and 1 is option there. then we can put first or 1 into input box without using sql and ajax.
use only jquery for this
$(document).ready(function(){
$("#my_select").change(function(){
var thisval = $(this).val();
$("input").val(thisval);
});
});
Related
I have tied for hours now to get the values value="oneyearanalysis" and value="oneyearanalysis", of each button when i press them, i tried with ajax. I think i am close but i get no output GetData but i do in my console.log(clikYear);. Hope you can help me
Best regards
Index.php
<div class="container" style="margin-top: 50px;">
<select name="button" id="button">
<option value="">Select...</option>
<option value="oneyearanalysis">1 year analyse</option>
<option value="fiveyearanalysis">5 year analyse</option>
</select>
</div>
$(document).ready(function(){
$("#button").on("change",function() {
var clikYear = $(this).val();
console.log(clikYear);
if (clikYear !== "") {
var getTableCall2 = $.ajax({
url : "GetData.php",
type:"POST",
cache:false,
data:{clikYear:clikYear},
})
}
});
getTableCall2.done(function(data){
console.log(data);
console.log(getTableCall2);
console.log(clikYear);
})
GetData.php
if(isset($_POST['clikYear']))
{
$year= $_POST['clikYear'];
}
$yearOfChoice = $year;
You were quite close to make it work. If you apply the simple recommandations below, you should save yourself that kind of trouble in the future.
First recommandation is to indent your code correctly.
You would have noticed the getTableCall2 variable is declared on select change. But "used" before that when trying to apply the .done() promise handler.
Below, I made no change to you code except the indentation.
The order of execution on document ready is:
Register the change handler (not executed)
Throw an error.
$(document).ready(function(){
$("#button").on("change",function() {
var clikYear = $(this).val();
console.log(clikYear);
if (clikYear !== "") {
var getTableCall2 = $.ajax({
url : "GetData.php",
type:"POST",
cache:false,
data:{clikYear:clikYear},
})
}
});
getTableCall2.done(function(data){
console.log(data);
console.log(getTableCall2);
console.log(clikYear);
})
Second recommandation is to use the console where you would have seen this error:
ReferenceError: getTableCall2 is not defined.
Third recommandation is to be a bit more creative with you class and id naming. .button used on a <select> just can lead to confusion. You can use the name you want. Make it meaningful! I'm sure you do not use the word door when you talk about a window in the real life...
So here is something that should work:
$(document).ready(function(){
$("#analysis_term").on("change",function() {
var clikYear = $(this).val();
console.log(clikYear);
if (clikYear !== "") {
var getTableCall2 = $.ajax({
url : "GetData.php",
type:"POST",
cache:false,
data:{clikYear:clikYear},
})
getTableCall2.done(function(data){
console.log(data);
})
}
});
});
And to test it, make sure the GetData.php outputs something in both cases:
$year = "No POST value!";
if(isset($_POST['clikYear'])){
$year= $_POST['clikYear'];
}
echo "The POST value is:" . $year;
The ID for your Select is "button" but you are using Class Selector instead. Try replacing . with # in front of button
HTML
<select id="button">
<option value="">Select...</option>
<option value="oneyearanalysis">1 year analyse</option>
<option value="fiveyearanalysis">5 year analyse</option>
</select>
JQuery
$("#button").on("change", function () {
var clikYear = $(this).val();
console.log(clikYear);
if (clikYear !== "") {
$.ajax({
url: "GetData.php",
type: "POST",
cache: false,
data: { clikYear: clikYear },
})
}
});
I want to get a jquery (a selectmenu) variable to php. I try it with Ajax "POST". For testing purpose i just want to echo out the selected number of the selectmenu without any page refresh. So the Change should appear dynamically.
Here is my html_file.php
<script> $( function() {
$( "#number" )
.selectmenu()
.selectmenu( "menuWidget" )
.addClass( "overflow" );
$.ajax({
method: "POST",
url: "php_file.php",
data: { number }
})
/* Here we receive the data back */
.done(function(data) {
/* Here you can do whatever you want with the data */
$("#response").html(data);
});
} );
</script>
<select name="number" id="number">
<option>1</option>
<option selected="selected">2</option>
<option>3</option>
</select>
<div id='response'></div>
And here the php_file.php
<?php
$test = $_POST['number'];
// Output of the selcetmenu for testing
echo '<div class="profile-font2">', $test, '</div>';
?>
I think there is some mistake in the Ajax function.
Pass key as number and assign value to number key as
$.ajax({
method: "POST",
url: "php_file.php",
data: { number : yournumber }
})
You have to give your data property a value.
Try:
$.ajax({
method: "POST",
url: "php_file.php",
data: {
number: $("#number").val()
}
})
I have an HTML form and it contains basically of one input text box and one select...
Input box
echo '<input type="input" id="category_typex" name="category_typex" value="" />';
Select
echo '<select name="primary_cat" id="primary_cat" onChange="getSecondaryCat(this.value);">';
The Ajax code I'm using to pass the select is as follows:
function getSecondaryCat(val, cat_var) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:'secondary_cat='+val,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
I'm not being able to pass both the select and input values onto the next page....
I'm only able to pass the select value and not the input value
Try this:
function getSecondaryCat(val, cat_var) {
var category_typex = $('#category_typex').val();
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:{secondary_cat:val, category_typex:category_typex},
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>');
}
});
}
this is my Jquery:
$('#Save').click(function () {
var realvalues = new Array(); //storing the selected values inside an array
$('#Privilege :selected').each(function (i, selected) {
realvalues[i] = $(selected).val();
});
$.ajax({
type: "POST",
traditional: true,
url: "http://localhost:8081/crownregency/UpdateOrCreateOrDeleteUser.php",
data: {
Privilege: realvalues,
ID: '1'
},
success: function (data) {
alert(data, 'Status');
location.reload();
}
});
});
this is my php.
I have read quiet a lot about serializing but doesnt seem to work, what i am trying to achieve is sending the selected items of a dropdown into an array and sending it to a php through ajax. but sending the array to the php doesnt seem to work. help anyone?
Your code is okay just remove the traditional: true and your code seems to work
$('#Save').click(function(){
var realvalues = new Array();//storing the selected values inside an array
$('#Privilege :selected').each(function(i, selected) {
realvalues[i] = $(selected).val();
});
$.ajax({
type: "POST",
url: "http://localhost:8081/crownregency/UpdateOrCreateOrDeleteUser.php",
data: {Privilege: realvalues, ID: '1'},
success:function(data){
$("#subscrres").html(data)
}
});
});
HTML
<form method="post">
<select id="Privilege" multiple="multiple">
<option value="yahoo">yahoo</option>
<option value="chrome">chrome</option>
<option value="mozilla">mozilla</option>
</select>
<input type="button" id="Save"/>
</form>
UpdateOrCreateOrDeleteUser.php
<?php
if(isset($_POST['Privilege'])){
$myvar =$_POST['Privilege'];
foreach($_POST['Privilege'] as $one)
echo $one."<br/>";
}
?>
i have a problem when it comes to retrieving value from jQuery to php.i was able to get the value of my select and pass it to my php but i can't pass it back to php. here is the code...
<script>
$(document).ready(function()
{
$("select#months").change(function(event)
{
var m=$(this).val();
$.ajax({
type: 'POST',
url: "monthly_CRD.php",
data: {m: m},
success: function(){alert("updated")}
});
});
});
</script>
<div>
<select id="months">
<option value='00'>Month...</option>
<option value='01'>Jan</option>
<option value='02'>Feb</option>
<option value='03'>Mar</option>
<option value='04'>Apr</option>
</select>
<select id="years">
<?php
for($yr=10; $yr<=$year; $yr++)
{
echo "<option value='".$yr."'>".$years[$yr]."</option>";
}
?>
</select>
</div>
<?php
if (isset($_POST['m']))
{
$m = $_POST['m'];
echo $m;
} else {echo "fail";}
?>
it keeps on returning fail which means that isset is not working.
Change data: {m: m} to data: {"m":m}
Since you are looking at $_POST['m'] you need to define that key in your JSON. Currently you'd need to look inside $_POST['03'] if you selected Mar
If you mean that on page load, it returns fail, that is because on page load your $_POST array is probably empty.
If you want to know what was returned from your AJAX post, you need your success function to accept a parameter (like data) that jQuery will fill with the response to your post.
Then in the function body, you can write it to the DOM or your error console.
If you have an id to an element then just id is enough to select. Try this
$(document).ready(function()
{
$("#months").change(function(event)
{
$.ajax({
type: 'post',
url: "monthly_CRD.php",
data: { m: $(this).val()},
success: function(){
alert("updated")
}
});
});
});
$(document).ready(function()
{
$("#months").change(function(event)
{
$.ajax({
type: 'post',
url: "monthly_CRD.php",
data: '{ "m": "' + $(this).val() + '"}',
success: function(msg){
alert(msg)
},
error: function(msg) {
alert("An error happened: " +msg);
}
});
});
});
User fiddler of chrome tools to break on either success or error and check the value of the meesage property.