0

I am trying to get the response from the outside file. In the php file at the moment there is only one variable echoed and I would like to display it inside #code div. How can I do this?

 $(function(){
            $('#submit').click(function(e){
               var length = $('#number').val();
               var type = $('#type').val();
               var submit = "submit";
               var url = 'public/php/codegenerator.php';
               var data = "length=" + length + "&type=" + type + "&submit=" + submit; 
               console.log(data);

               $('#code').load(url, data, function(response){
                   $(this).response.hmtl();
               });

             e.preventDefault();
            });
        });

4 Answers 4

1

load should do it for you. So changing it to this, should work

$('#code').load(url, data, function() {
    alert('success');
});
Sign up to request clarification or add additional context in comments.

2 Comments

This is working. How can I get the variable from the PHP file?
Load is the simplest way of fething data from the server, used mostly for getting whole html sets and just inserting them in the page. If you would want to get variables back and call them e.g. response.myphpvariable, I suggest you check out api.jquery.com/jQuery.getJSON this method. Instead of printing your php variable, you will encode it in json and you can use it as you please in your js code
0

Try this

$('#code').load(url, data, function(response){
         $(this).html(response);
});

OR

$('#code').load(url, data);

Comments

0

try this code peace

<div id="success"></div>
<div id="error"></div>

<script>
$("#success").load(url, data, function(response, status, xhr) {
  if (status == "error") {
    var msg = "error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});
</script>

Comments

0

So, you're doing mostly the right things if your php code is sending back the content type "text/html". You might try setting the response's content-type in your PHP code. If you can't figure out how to change the content type, or don't want to, use the jQuery.get() method and change the TEXT(not html) of the #Code element by $("#Code).text(yourResponse);

You might also try accessing the xhr.responseText to see the whole response. This is always an invaluable tool to use when figuring out what's going wrong:

$.ajaxSetup ({
error: function(x, e){

        alert("The Response:" + x.responseText);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.