I write this code, but it doesn't work. I want to show an array in php using ajax.
It's an html select that chooses every option of this select list value of the option take in the variable and sends it to ajax. Ajax should then post data to php then php select the received data from database and show all of them.
But I can't show this data in ajax. :(
$(function(){
$("#topic").change(function(){
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
options(str);
});
});
});
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}).done(function(){
$("#content").html(data);
alert("ok");
});
});
}
You have an error in your ajax request.
Here the right code:
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}
}).done(function(data){
$("#content").html(data);
alert("ok");
});
}
Related
I am using jquery to call a PHP script when a select box changes. I am able to get the ID of the select item that changes, however I get no output from the PHP script. The Javascript is
function initagents() {
$('select.agent').on('change', function() {
var id = $(this).attr("id");
console.log(id);
var data = { id: id };
$.ajax({
type: "POST",
dataType: "json",
url: "updateagent.php",
data: data
}).done(function(msg) {
console.log(msg);
});
});
}
and the PHP is
print_r ( $_POST);
Any ideas?
Please use
echo json_encode($_POST['id']);
instead of
print_r($_POST);
You have made a mistake by placing the onChange event of the select box inside function initagents().
fixed code:
$('select.agent').on('change', function() {
var id = $(this).attr("id");
console.log(id);
var data = {
id: id
};
$.ajax({
type: "POST",
dataType: "json",
url: "updateagent.php",
data: data
}).done(function(msg) {
console.log(msg);
});
});
Just try with the following code:
function getID(val) {
alert(val);
$.ajax({
type: 'POST',
url: 'post.php',
data: {
id: val
},
success: function(data) {
alert(data);
}
});
}
<select name="pks" onchange='getID(this.value);'>
<option value="1">Option1</option>
<option value="2">Option2</option>
</select>
Seems your code is correct and it should send the data to php file. But also I see that, you are not sending selected value to php script. You are posting selected element ID.
Are you trying this in a sub folder ? make sure the php file path is correct in javascript. Better if you can try with the full path. For example
url: "http://localhost/myproject/updateagent.php",
I know this has been asked before, but I can't get it working.
I'm loading a file on a page whose path and name are a variable contained in a PHP script.
My script.php is outputing a variable $filename which contains the path to a file that has to be open in an ajax request.
So, the file can be for example:
'../path/to/file/filea.json' or
'../another/path/fileb.json'
I tried this in my jQuery:
$.ajax({
url:'script.php',
success:$.ajax({
url: ??? // This ($filename) is what I'm trying to get from the 1st Ajax call
sucess: function(data){
//other code here
}
})
);
I know $filename is wrong in the second Ajax call but how do I get the value of that variable?
$.ajax({
url: "script.php",
success: function(data){
$.ajax({
var someParam=data; //response Data from 1st Ajax Call
type: "post",
url: "example.php",
data: 'page='+someParam,
success: function(data){
//Do More Here!
});
});
Try this:
inside script.php
echo json_encode($filename); //somewhere you need to have this echo.
jQuery
$.ajax({
url: 'script.php',
success: function (data) {
alert(data); //or console.log() and doublecheck you have the right info
$.ajax({
url: JSON.parse(data),
success : function (data) {
//other code here
}
})
}
}); // added a extra } to close the ajax
This solved the problem:
$.ajax({
async:false,
url: 'script.php',
success: function(data){
$.ajax({
url: data,
success: //code here
})
})
#Sergio: script.php wasn't echoeing the output of $filename... Thanks for reminding!
I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
AJAX CODE:
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......
Right now I've got a ui dialog on a page, and I need some way to pass a javascript variable to the php within the dialog via AJAX. Here's my code:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").dialog('open');
}
});
});
and the beginning of the PHP:
<?php
if(isset($_POST['user'])){
echo '<center><b>User: '.ucfirst($_POST['user']).'</b></center><br />';
}
The problem is, it's just not getting passed. I'm very new with Ajax so I'm sure I'm messing something up.
This is not working because you don't specify the return variable, see your code with correction:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(data){ // Here you specify the callback variable from the AJAX call
alert(data); // Here will show '<center><b>User: UserExample </b></center><br />';
$("#dialog").dialog('open');
}
});
});
You can make your dialog content my loading some page like Userpage.php and then pass User as a url parameter
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").load('/userpage.php?User=' + user).dialog('open');
}
});
});
I was wondering if this was making an asynchronous request...write now Im using:
<script type='text/javascript'>
$(document).ready(function() {
var ktitle = $('.hiddentwo').text();
$('div#tab2').load('morefour.php?title=' + encodeURIComponent(ktitle));
});
</script>
what Im doing though is adding text in the first, into the database, on the current php file (addtext.php). Im passing the Id of the current document to the morefour.php and that is loading the added text on the second tab...the thing is, Im having to refresh to see the content again. Im running on localhost btw.
For more clarity, Im running another jquery script that on clicks, retrieves this data to send it to a php file to enter into a database
$(".button").click(function() {
var content = $(this).siblings().outerHTML();
$.ajax({
async: false,
type: "POST",
url: "tosqltwo.php",
data: {
content: content
}
});
});
$(function(){ //shorthand of $(document).ready
$('div#tab2').html($.ajax({
type: "GET", //if you are doin $_GET['title'] in morefour.php
url: "morefour.php",
data : {title:ktitle},
dataType: 'html', //i am not sure about this part
async: false
}).responseText)
});
or you can try
$(function(){
$.ajax({
url : 'morefour.php',
data : {title:ktitle},
type:'GET',
dataType:'html',
success: function(data) {
$('div#tab2').html(data);
}
});
});
you can use $.ajax function with async to false.
$.ajax({
async: false,
url : 'morefour.php',
data : 'title=' + encodeURIComponent(ktitle),
success: function(data) {
$('div#tab2').html(data);
}
});