Rectangle 27 0

<?php
     function dateRangeTimeFrame($var1){
      ...

      $date['startDate'] = $startDate;
      $date['endDate'] = $endDate;

      return json_encode($date);
     }

?>
$.ajax({
    url: 'includes/functions.php',
    type: 'post',
    data: { "dateFunction": theDate},
    dataType: "json",
    success: function(response) { 
       console.log(response.startDate); 
    }
});

javascript - jQuery access Ajax Response from PHP function - Stack Ove...

javascript php jquery
Rectangle 27 0

<?php echo $commentcreatetimearr[$key]; ?>
$.ajax({    
  url: 'wall_list.php',
  data:"dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
  type: 'POST',
success: function (resp) {     
  if(resp){
     $('#wall_listdiv').html(date_cal(resp));
  }

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - Calling javascript function inside jquery ajax response file cod...

php javascript jquery ajax response
Rectangle 27 0

You can't triggure something on the client-side with server-side code like php however jQuery has a success method that executes after an ajax call has been made.

$.ajax({
  url:"someurl.php",
  success: function()
  {
     $('yourSelector').yourfunction("");
  }
});

Additionally you can pass form data to php pages and return html/javascript or jason and either inject it to the page or parse/run the code. heres some usefull links AJAX documentation: http://api.jquery.com/jQuery.ajax/ w3cschool example with autocomplete text field: http://www.w3schools.com/php/php_ajax_php.asp

Trigger jQuery function from PHP response - Stack Overflow

php jquery ajax
Rectangle 27 0

Depends on whose security you're are talking about, the webserver or the client. From what you have listed in the code, the server only sends data to the AJAX client, so it is not susceptible to a script injection attack since it is not acting on received scripts. (Although it is probably parsing a GET/POST response, but that is a different topic)

The fact that you use smarty doesn't make a difference with security, it is only a templating tool.

Javascript on the client is inherently insecure. If you're that worried about script injections coming back to the client, don't use javascript. As an alternative, you can try to strip any of the returned AJAX data to remove HTML or embedded scripts.

jquery - Is a good idea use template function in my site that retrieve...

php jquery ajax smarty response
Rectangle 27 0

If your assessment.php make a response as follows:

<?php
       echo 'successfully_completed';
  ?>

The response data from PHP will be passed as follows:

$.ajax({
    url: url, // assessment.php
    data: assessrows,
    type: "POST",
    success: function(data) {
        alert("The response is [" + data + "]"); 
        // "The response is [successfully_completed]"

        // If you want to pass this data to the next page
        window.location.href = "/foo/success.php?data="+data;  
    }
});

You're right, I shouldn't have gone clogging up the global namespace. I'm not really gonna do anything else with it, but you're still right. Also, I had it as a variable when I wrote it, but I was testing stuff & it was a string when I copied it into the question. Will change it back.

The linked question talked about how you return the data on a JavaScript page, but what I'm asking is how to return it on a PHP page.

Oh, I'm sorry. Do you mean you are asking the way to send data from javascript to PHP?

No, I'm already sending it. If you notice, the URL goes to a PHP page, not a JavaScript page. I'm asking how I get the data variable once I'm there.

In your code, You are posting data to the url and jumping to the same url after sccessfully completed. Could you elaborate on that? Why you need this.

AJAX Function jQuery to PHP Access Response Variables - Stack Overflow

php jquery ajax json
Rectangle 27 0

$.ajax({
    url: "url",
    data: assessrows,
    type: "POST",
    success: function(data) {
        console.log(data); //do something with data
        window.location.href = url;
    },
});

Should I do something with data in the success function before it goes to the PHP page? Will that help me access it from there?

I did not know you wanted to do that, but it looks like @naoki already has it covered.

Sorry, I'm still pretty new to AJAX, so I don't really know what you can & can't do with it.

AJAX Function jQuery to PHP Access Response Variables - Stack Overflow

php jquery ajax json
Rectangle 27 0

instead of placing javascript code in the php return, why dont you place it in the .post response?

$("input[id*='post_video_comment']").click(function() {           
$.post('video_comment.php', function(response) {
  $('#comment').html(response.code);  
    //place javascript code here
}, "json");
onload
js
$str = '<div id="commentplayer"></div>
<script type="text/javascript">
window.onload=(function() {
    jwplayer("commentplayer").setup({
    file: "...url",
    width: "300",
    height: "225",
    provider: "http",
    startparam: "start",
    wmode: "transparent",      
    controls: "true", 
    stretching: "uniform",  
    primary: "flash",     
    autostart: "false",
    ga: {}
    });
});
</script>';

echo $str;

I can't place the javascript code in post response, because the javascript function is display within a text. e.g: text... javascript ...text...

@richard i see, can you share the js inside the php then? do you have a onLoad call?

@ tq, i've updated the javascript in my question. It's a jw player.

I just tried your onload solution, but the problem is, it's just show up "text...<div id="commentplayer"></div> text...", everything inside <script type="text/javascript"></script> missing.

right now it looks like you have response.msg which means that the response is looking for msg, can you use just response?

php - Javascript function response in Jquery not working - Stack Overf...

php javascript jquery ajax
Rectangle 27 0

The problem is that you're binding it to a click on all elements with the class ".but", if you on the other hand gave the button an id like

echo '<input data-pid="'. sha1($product_id) . '" class="but" type="button" value="Add Cart" />';

and then in your jquery function would do the following (outside the loop):

<script>
$(document).ready(function(){
  $('.but').click(function(){
    $.ajax({
      type: 'POST',
      url:"addcart.php",
      data : $(this).data(),
      success:function(result){
        $("#div1").html(result);
      }
    });
  });
});
</script>

Then you would get around the problem, as it then listens for the click event on each element, but the data is varying on each element.

JQuery Function not response inside of php while loop - Stack Overflow

php jquery
Rectangle 27 0

eval
success: function (response) {
    eval(response);
}

Just as a note, make sure you 100% trust the source of your response to not send you anything malicious here.

Running the response of a jQuery .Ajax call (which is a string from a ...

php jquery ajax
Rectangle 27 0

<?php
function dateRangeTimeFrame($var1) {
   // ...

   $date['startDate'] = $startDate;
   $date['endDate'] = $endDate;

   echo json_encode($date);
}
?>
$.ajax({
   url: 'includes/functions.php',
   type: 'post',
   data: { "dateFunction": theDate },
   success: function(response) { 
      for (var i = 0; i < response.length; i++) {
         alert(response[i].startDate);
      } 
   }
});

javascript - jQuery access Ajax Response from PHP function - Stack Ove...

javascript php jquery
Rectangle 27 0

Try using echo in your PHP instead of return.

echo $printing;

My jQuery, AJAX call is getting a null response on a valid return of w...

php jquery ajax codeigniter google-chrome
Rectangle 27 0

here you go

$.ajax({
    url: 'wall_list.php',
    data: "dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
    type: 'POST',
    success: function (resp){
        if(resp){
            $("#wall_listdiv").html(resp);
        }
    },
    dataType: 'html'
});

What you want to do is, specify the return dataType as html. From jQuery API

If html is specified, any embedded JavaScript inside the retrieved data is executed before the HTML is returned as a string. Similarly, script will execute the JavaScript that is pulled back from the server, then return the script itself as textual data.

php - Calling javascript function inside jquery ajax response file cod...

php javascript jquery ajax response
Rectangle 27 0

in you loop ,.but was bind an function each time,so the last bind one works .try to give the .but elements an unique id such as "id=$product_id";

and you can bind the function once (outside the loop).this simply like:

var data = $(this).attr("id");
$.ajax({

    type: 'POST',
    url:"addcart.php",
    data : { pid : data},
    success:function(result){
        $("#div1").html(result);

}});

JQuery Function not response inside of php while loop - Stack Overflow

php jquery
Rectangle 27 0

You are repeating the entire Javascript for every product. Move the Javascript out of the loop and set the product ID as a data-productid attribute on the button:

echo '<input class="but" type="button" value="Add Cart" data-productid="' . htmlspecialchars($product_id) . '" />';

Then when you make the ajax call (on click), read the product ID and set it as the POST data:

$.ajax({
    type: 'POST',
    url:"addcart.php",
    data : { pid : $(this).data('productid') },
    success:function(result){
        $("#div1").html(result);
    }
});

Nice, working fine......thanks a lot...

JQuery Function not response inside of php while loop - Stack Overflow

php jquery
Rectangle 27 0

echo '<input class="but" type="button" rel="'.htmlspecialchars($product_id).'" value="Add Cart" />';

and your script :

<script>
$(document).ready(function(){

  $('.but').click(function(){


    $.ajax({

        type: 'POST',
        url:"addcart.php",
        data : { pid : $(this).attr('rel'),
        success:function(result){
      $("#div1").html(result);

    }});

  });

});

</script>

Yes, this would be correct but do not put the jquery code inside the loop !

ya i changed that method rahul....

JQuery Function not response inside of php while loop - Stack Overflow

php jquery
Rectangle 27 0

At the beginning of your PHP code try to put this :

ob_start();
ob_end_flush();

Jquery PHP without response function - Stack Overflow

php jquery ajax
Rectangle 27 0

I would suggest opening up Developer Tools in Chrome (View > Developer > Developer Tools) and selecting the Network tab. When your AJAX post request is made, it should add an entry there (the "Path" column should be "index.php/controllerFile/get_standby" and the "Method" column should have "POST"). Click the row for the request and check the Response tab to make sure your JSON is there.

If the response is empty, your problem is with your PHP code (you might not be printing the JSON returned from that function to the page). Otherwise, it would seemingly be a problem with your JavaScript.

My jQuery, AJAX call is getting a null response on a valid return of w...

php jquery ajax codeigniter google-chrome
Rectangle 27 0

I recommend reading this questions answer, then return the string (or echo to out)

<script type="text/javascript">$( "#new-cat-input" ).dialog( "open" );</script>

Actually, I retract my answer as this will lead to either not knowing when the script is executed or to having to eval this script to execute it.

Stick with sending an ajax-request and handling the response as suggested by others.

maybe you need to distinguish between calls that return entire pages and calls that just return some data or page fragements (which you can append to divs)

Trigger jQuery function from PHP response - Stack Overflow

php jquery ajax
Rectangle 27 0

The problem is you expect that Javascript contained within an ajax response is executed. This isn't the case, the browser doesn't execute any Javascript contained within ajax responses. It might be possible to try to parse out the Javascript and execute it in some way, such as eval() but that would be nasty and not a good idea. Using eval() you also have to consider that it will only accept valid Javascript, so you couldn't just pass your response to it because that includes some HTML.

A possible solution could be to have the popup() function already defined in the page or an external Javascript file, and to assign the click handler after you add the HTML to the DOM.

function popup()
{
    alert(this.id);
}


document.getElementById("alldata").innerHTML = response;
document.getElementById("myNewLabel").onclick = popup;

hii, i implemented same thing which you told, but i got one problem , i want to pass id of label in popup function so how to do that? i tried like document.getElementById("myNewLabel").onclick = popup(this.id); but its not working :(

You don't need to pass the ID, just use this.id within the popup function.

php - javascript function inside jquery response not working - Stack O...

php javascript jquery
Rectangle 27 0

Make that string hidden in your page and just display it on success. like:

<label onclick="popup()" id="mylable" style="display:none"> clickme </label>
<script>
function popup()
{
  alert("hello");
}
</script>


var url = "showpdf.php";
$.ajax({
    type: "post",
    url: url,
    data:    {academic:academic,uni:uni,course:course,lan:lan,sem:sem,subject:subject,type:type,clz:clz},
    success: function(response)
    {
       // alert(response);
        $("#mylable").show();

    }
});

Better way to try with DOM. Let me know if above code is work for you.

hey but <label onclick="popup()" id="mylable" style="display:none"> clickme </label> <script> function popup() { alert("hello"); } </script> this code i wrote in another file, so $("#mylable").show(); will not work der in jquery function

Keep the HTML code on same page form where you are calling the ajax function and echo something like "click"/ notclick in showpdf.php file. Check the response in success function if it is click $("#mylable").show().

i couldn't understand what u r saying

php - javascript function inside jquery response not working - Stack O...

php javascript jquery