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();
      }
    });
  });
}
jockebq
  • 191
  • 9
  • 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. – Canser Yanbakan Mar 26 '19 at 13:56
  • How is the wrap done? – jockebq Mar 26 '19 at 13:57
  • Just; replace `$(location)` to `$('
    '+location+'
    ')`.
    – Canser Yanbakan Mar 26 '19 at 13:58
  • Tried that, but still nothing is displayed, I tried `$('

    Text

    ')` to make sure it worked first. And then `$('

    '+location+'

    ')` and I know location contains data. But do I need to declare it some other way to be able to use it in `success`
    – jockebq Mar 26 '19 at 14:02
  • Can you check location value like console.log(location)? – rajeev Mar 26 '19 at 14:04
  • Please complete your function with writing the all function so we can see the all story. You are passing location to your searchEmail function and calling that ajax in it right? So definitely location must have a value. – Canser Yanbakan Mar 26 '19 at 14:04
  • Yes, exacly, I pass `location` to `searchEmail` and later pass location to a PHP function called `search_notify_email` where I make it a PHP variable `$location = $_POST['location'];` and I get this sent in an email. And there the variable is not empty. I cannot add much more info to the first post. – jockebq Mar 26 '19 at 14:11
  • Updated with the complete jQuery function – jockebq Mar 26 '19 at 14:36

2 Answers2

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();
  }
 });
});
  • You should also escape the location variable: [escape a php sting for a javascript variable](https://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – maxpelic Mar 26 '19 at 14:02
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);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101