When I try to send a var with jquery, ajax php doesn't receive it, and I don't know why.
This is my script:
function showHiddenUserInfoPanel(id){
$(document).ready(function(){
$('#admin-resume').fadeToggle('slow', 'linear', function(){
$.ajax({
async: true,
type: "POST",
dataType: "html",
contentType: "application/x-www-form-urlencoded",
data: "userid="+id, // Also I tried with : data: {userid: id},
url: "user-profile.php",
success: function(){
$('#info-user').fadeToggle('slow', 'linear');
alert(id); // I receive the correct id value
}
});
});
});
And this is my user-profile.php:
<div id="activity_stats">
<h3>Viendo el perfil de <?php echo $_POST["userid"]; ?></h3>
</div>
Can anybody can help me?
Thanks :)
Is it just me or you are not using the response at all?
$.ajax({
success: function(){
$('#info-user').fadeToggle('slow', 'linear');
}
});
Your anonymous success function is ignoring all responses. I suspect your #info-user should be an empty element and you are not getting a visual feedback.
An example could be:
$.ajax({
success: function(response){
$('#info-user').html(response).fadeToggle('slow', 'linear');
}
});
Other alternative could be using .load().
Related
I am trying to echo back code I received from Ajax. But it does not work when I am using contentType:false, processData:false.
Here is my ajax. The url is correct. If I comment out the line with post_data['file'] and contentType:false, processData:false I will be able to get the echo, but as soon as contentType:false, processData:false is in I cannot get anything.
post_data = {};
post_data['file']= document.getElementById('fileToUpload').files[0];
post_data['paper-type']=$("#paper-input :selected").val();
$.ajax({
url:'/admin/upload_paper',
data: post_data,
type: 'post',
contentType: false,
processData: false,
success: function(data){
console.log(data);
},
error: function(data){
console.log(data+"error");
}
});
Here is the code snippet from CI
public function upload_paper(){
echo $this->input->post('paper-type');
echo "testing";
echo "testing2";
}
Does anyone know what that is? Thank you.
instead of doing this:
post_data['paper-type']=$("#paper-input :selected").val();
try
var val = $("#paper-input :selected").val();
and in ajax:
$.ajax({
url:'/admin/upload_paper',
data: {'paper-type':val},
type: 'post',
success: function(data){
console.log(data);
},
error: function(data){
console.log(data+"error");
}
});
I'm trying to make an app where if you click on a div, the data-id will be put into a variable, which will give PHP the select parameters to look for. Here's my code to better explain it.
HTML:
<div class="box" data-id="caption"></div>
JQuery:
$('.box').click(function () {
var caption = $(this).data('id');
});
After Googling I found the best way to do this is through AJAX, which I then proceeded to try:
$.ajax({
url: 'index.php',
type: 'GET',
dataType: 'json',
data: ({ caption }),
success: function(data){
console.log(data);
}, error: function() {
console.log("error");
}
});
However, this doesn't seem to work. If there's a better way to do what I mentioned above, I'm open to new ideas.
EDIT
Here is my PHP code.
if(isset($_GET['caption'])){
echo $caption;
$select = "SELECT * FROM pics WHERE text = '".$caption."'";
}
?>
Look through the api at jQuery.
Your data key should contain a json object -
$.ajax({
url: 'index.php',
type: 'GET',
dataType: 'json',
data: {caption: caption},
success: function(data){
console.log(data);
}, error: function(error) {
console.log(error);
}
});
here is my simple code in trying to get data from php to ajax.
i just wanted to get a simple data pass back to ajax success. i already searched for this but i cant get it properly. my code is really simple.
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<SCRIPT TYPE="text/javascript">
$(document).ready(function(){
$("#1").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { "txt": "try"},
cache: false,
success: function(html)
{
alert(html.mes);
}
});
});
});
</SCRIPT>
<pre><button id="1">try</button></pre>
Then i load a ajax.php that has a simple code too
$var['mes'] = 'message';
echo json_encode($var);
and its alerting me "undefined". i know this is simple but i cant find it out where do am i wrong
You need to tell jQuery that the script is running JSON:
$(document).ready(function(){
$("#1").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { "txt": "try"},
dataType: 'json',
cache: false,
success: function(html)
{
alert(html.mes);
}
});
});
});
I have a an script that sends a post to my php file:
$('.target').change(function() {
$.ajax({
type: "POST",
url: "/form-actions.php",
data: {rechten : '0'},
cache: false,
success: function(){
alert("Submitted");
}
});
});
When i use firebug i can see the post being send: Parameter: rechten 0
But my form-actions.php (which is in the right location) can't see the post when i use
<?php print_r($_POST); ?>
The outcome of this is Array ( )
What am i doing wrong?
Thank you for your time!
(I'm adding this as an answer rather than a comment because of the code)
Are you super-sure your php file is in the right place?
Change your JS to
$('.target').change(function() {
$.ajax({
type: "POST",
url: "/form-actions.php",
data: {rechten : '0'},
cache: false,
success: function(data){
alert(data);
}
});
});
And see what is alerted.
This is assuming that form-actions.php contains just
<?php
print_r($_POST);
?>
and nothing else (or you'll see that too).
I want to send the data via ajax to other page. I have isolated the problem. This is the code.
Thank you all for your help..But no effect..
updated code
It worked...
<script>
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
var size=123;
var itemname=123;
var potency=123;
var quantity=12333;
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
success: function(data) {
alert(data);
},
error: function(data) {
alert(data);
}
});
});
});
</script>
So I click the link,it navigates, to dd.php which has
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']));
echo $_POST['itemname'];
?>
I get Object Object as alert. What am doing wrong? Pls throw some light here..thanks you..
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault();
var data = {"box":1233,
"size":565,
"itemname":565,
"potency":876,
"quantity":234};
$.ajax({
url: "dd.php",
type: "post",
data: data,
dataType: "json",
success: function(data) {
if(console){
console.log(data);
}
},
error: function(data) {
if(console){
console.log(data);
}
}
});
});
});
few things to consider... you can post data as object..which is clean and easier to use
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
....
var dataString ={'box':box,'size':size,'itemname':itemname,'potency':potency,'quantity':quantity};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json", //<--- here this means the response is expected as JSON from the server
success: function(data) {
alert(data.itemcode); //<--here alert itemcode
},
error: function(data) {
alert(data);
}
});
so you need to send the response as json in PHP
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Here you are using querystring as sent in GET request.
If you want to send the data in same form, you can use this with GET request type:
$.ajax({
url: "dd.php"+dataString,
type: "get",
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
Or for POST request,you will have to put data in json object form, So you can use :
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
});
And put echo in your php code :
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Javascript alert shows [Object object] for object. You can see response using console.log or can use that key with alert.
For more information, refer jQuery.ajax()