使用JQuery ajax调用PHP文件,进行处理,但保持在同一页面上 [英] Using JQuery ajax to call a PHP file, process, but stay on same page

查看:133
本文介绍了使用JQuery ajax调用PHP文件,进行处理,但保持在同一页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在实际站点上有效(按原样).我试图通过XAMPP在localhost上运行它,但未调用addToCart.php.

This code is working (as is) on a live site. I am attempting to run it on localhost via XAMPP but addToCart.php is not being called.

想知道是否有更好的方法可以编写此代码,因此它更健壮,并且也可以在localhost配置上使用.从代码中可以明显看出,我对JQuery或PHP不太熟悉.

Wondering if there's a better way to write this so it is more robust and will work on localhost config as well. I am not very familiar with JQuery or PHP, as may be obvious from the code.

<a id="atcAnchorTag'.$thisProduct['id'].'" 
href="#" 
onclick="
jQuery.ajax(\'./addToCart.php?id='.$thisProduct['id'].'\');
jQuery(atcAnchorTag'.$thisProduct['id'].').hide();
jQuery(rfcAnchorTag'.$thisProduct['id'].').show();
jQuery(mt'.$thisProduct['id'].').hide();
jQuery(grn'.$thisProduct['id'].').show();
scItemsCountJS++ ;
return false ; "
style="display:'.$atcDisplayType.'">add to cart</a>

推荐答案

实际上,Blender是正确的,如果可以的话,我会把答案归功于他.按照他的建议重写代码并不是很困难,但是花了很长时间自己弄清楚.

Actually, Blender was correct and I would credit him with the answer if there was a way to do so. Rewriting the code as he suggested turned out not to be very difficult but it took a long time to figure it out myself.

这是起作用的. HTML:

Here is what worked. HTML:

<a id="atcAnchorTag'.$thisProduct['id'].'" 
    href="#" 
    style="display:'.$atcDisplayType.'">
    add to cart
</a>

jQuery:

<script type="text/javascript">
    $("#atcAnchorTag'.$thisProduct['id'].'").click(function() { 
        //alert("Hello");
        jQuery(atcAnchorTag'.$thisProduct['id'].').hide();
        jQuery(rfcAnchorTag'.$thisProduct['id'].').show();
        jQuery(mt'.$thisProduct['id'].').hide();
        jQuery(grn'.$thisProduct['id'].').show();
        scItemsCountJS++ ;
        var prodid = '.$thisProduct['id'].';
        $.post("addToCartJQ.php", {id: prodid}, function() {
        });
    });
</script>

这篇关于使用JQuery ajax调用PHP文件,进行处理,但保持在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆