简体   繁体   中英

Javascript Variable, Send to PHP Variable

I have a dynamic listing of products pulling from a MySQL Database. On the listing there is a Contact Now button which I am using a jquery Modal script for which pops up with a form.

My problem is trying to pass the product information variables to that popup.

I was thinking of adding the Brand and Model name using the rel attribue like this:

<a href="#" class="contact_now" rel="<?=$sku_brandname;?> <?=$sku_modelname;?>">Contact Now!</a>

and then with Javascript I can do this:

    $(".contact_now").click(function(){

    var rel = $(this).attr('rel');
    alert(rel);

which works perfect but on my Popup Div I'm not sure how to get that javascript rel variable to output it.

<p>Fill out the form below to inquire about the <strong>{ rel value here }</strong>.  We will get back to you as soon as possible.</p>

I'm not sure how to do this. If anyone has any ideas or suggestions I would appreciate it.

Thanks!

Few things. First of all, if you want to store data in some nodes, you're better off using data attributes, like this:

<a href="#" class="contact_now" data-brand-name="<?=$sku_brandname;?>" data=-model-name="<?=$sku_modelname;?>">Contact</a>

You can then use jQuery to get them like this:

$(this).data("brandName");

As for the sending the variable to PHP thing, you should instead modify the value with JavaScript when you open the popup. (If it's not on the same page, say that in the comment.). To do that, you can give a span a class and modify it before showing the popup.

<p>Fill out the form below to inquire about the <span class="product"></span> We'll get back to you ASAP.</p>

Then in jQuery:

$(".product").text($(this).data("brandName"));
//Show popup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM