1

I am trying to send a variable with PHP to jQuery where I want to use this variable to append to a specific div.

Here is how I send the variables to jQuery onclick : <a onclick="searchEmail('<?php echo $location ?>');">

Then I begin the jQuery function like this: function searchEmail(location) {

This how the ajax part in the function looks like, and it works perfectly, but I want to use the location variable for this part of the code: $(location).appendTo('#test'); so that I append the text in location to a specific <p with ID #test.

function searchEmail(email,title,content,location,siteurl) {
  $(document).off("click", "#confirm").on("click", "#confirm", function (event) {
    var $url = (admin_ajax.url);
    $.ajax({
      type: "POST",
      url: $url,
      datatype: "html",
      data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location, siteurl: siteurl },
      success: function() {
        searchNotification();
        console.log( location );
        $('<p>'+location+'</p>').appendTo('#test');
      },error:function() {
        searchNotificationError();
      }
    });
  });
}
8
  • If location is not a jQuery object, you can not append that to that div. You need to wrap it first, or change the append method with html method. Commented Mar 26, 2019 at 13:56
  • How is the wrap done? Commented Mar 26, 2019 at 13:57
  • Just; replace $(location) to $('<div>'+location+'</div>'). Commented Mar 26, 2019 at 13:58
  • Tried that, but still nothing is displayed, I tried $('<p>Text</p>') to make sure it worked first. And then $('<p>'+location+'</p>') and I know location contains data. But do I need to declare it some other way to be able to use it in success Commented Mar 26, 2019 at 14:02
  • Can you check location value like console.log(location)? Commented Mar 26, 2019 at 14:04

2 Answers 2

2

Why don't you create a variable in your jQuery code to store the location:

var location = '<?php echo $location; ?>';

$(document).off("click", "#confirm").on("click", "#confirm", function (event) {
var $url = (admin_ajax.url);
 $.ajax({
  type: "POST",
  url: $url,
  datatype: "html",
  data: { 'action': 'search_notify_email', location: location },
  success: function() {
    searchNotification();
    $(location).appendTo('#test'); /* or $('#test').append(location); */
  },error:function() {
    searchNotificationError();
  }
 });
});
Sign up to request clarification or add additional context in comments.

1 Comment

You should also escape the location variable: escape a php sting for a javascript variable
1

so that I append the text in location to a specific

If you've just text then you need instead of appending the text() method :

$('#test').text(location); //Note it will replace the original text

Or:

var p = $('#test');
p.text(p.text() + location);

4 Comments

Mind that, this is not appending. This is replacing.
Tried that, but it doesn't display anything at all. I know there is data in location, is there anything I am missing?
@jockebq try console.log( location ); just before the answer's line.
@ZakariaAcharki that comes up empty, I see it in the console log but it is just an empty line. Which is weird because the ajax part of this is working fine, and the location is transfered.

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.