0

I'm doing some ajax stuff which triggers on a click event, the link has a couple of values and variables.

The link in question looks like this:

<?php $link_url = esc_url(wp_nonce_url( site_url('?my_page=ajax-processor&action=dynamic_date_loop&my_date='.$date), 
"dynamic_date_loop_nonce") ); ?>

The $date variable above holds a string like "2013-09".

I process this link and extract the values here:

var url = wpAjax.unserialize(element.attr('href'));
var s = {};
s.data = $.extend(s.data, { action: url.action, _ajax_nonce: url._wpnonce });

I understand the syntax for this goes something like var1:value1 but here my value is a variable, so in this context do I just add my_date: url.$date ? This just doesn't look right to me.

1
  • You won't have access to your PHP variable $date when you're executing your javascript code. You'll have to extract it from the query string you've assembled and placed in the javascript variable url. Commented Sep 13, 2013 at 22:02

2 Answers 2

1

I think this is what you want:

s.data = $.extend(s.data, {
    action: url.action,
    _ajax_nonce: url._wpnonce,
    my_date: url.my_date 
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, so does url.my_date know that I mean, get the value that is inside the php variable $date?
No, it's getting it from the my_date=XXX parameter in the URL, just like url.action gets the action=dynamic_date_loop parameter.
0

No you need to store the value in javascript variable

var a = '<?php echo $date; ?>';

Now variable a contains the value of $date. Now you can do whatever you want with a. Check it by alerting alert(a);

1 Comment

okay, but this code takes place in a javascript file and link inside separate webpage. If I echo the php variable into a javascript variable that variable still needs to go into the link.

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.