传递JavaScript变量使用Ajax PHP [英] Passing Javascript Variable to PHP using Ajax

查看:139
本文介绍了传递JavaScript变量使用Ajax PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个现有的脚本,在其中使用了Ajax,这是我以前从未有过的那一刻。我有一个变量设置在我这得到它的值从输入字段我的页面上的JavaScript文件。我需要使用Ajax来发布这我的PHP页面只有我不知道从哪里开始,

林不知道code,你需要看到的,但我的JavaScript / AJAX code是,我需要传递变量是无功credoff

  $(的GetPoint)点击(函数(){
    VAR theid = $(本).attr(ID);
    VAR onlyID = theid.split(_);
    变种onlyID = onlyID [1];
    VAR credoff = parseInt函数($(本)。儿童(input.credoff:隐藏)。VAL());

    $阿贾克斯({
        网址:do.php,
        键入:POST,
        数据:用户ID =+ onlyID,
        成功:功能(数据){
            如果(数据=success1&放大器;!&安培;!数据=success5){
                $(#+ theid)的.text(数据);
            } 其他 {

                $(#thediv_+ onlyID).fadeOut(慢);
                $('#creditsBalance)淡出(慢)。
                NEWBALANCE = parseInt函数($('#creditsBalance)文本());
 

Wouldit必须在这种格式?

 数据:用户ID =+ onlyID,
credoff =+ credoff
 

解决方案

  ...
数据: {
    用户名:onlyID,
    credoff:credoff
},
...
 

I'm working on an existing script at the moment which uses Ajax, something I've never worked with before. I have a variable set in my javascript file which gets its value from an input field on my page. I need to use Ajax to post this to my PHP page only I've no idea where to start,

Im not sure what code you would need to see, but My javascript/AJAX code is, the variable I need to pass is 'var credoff'

$(".getPoint").click(function () {
    var theid = $(this).attr("id");
    var onlyID = theid.split("_");
    var onlyID = onlyID[1];
    var credoff = parseInt($(this).children('input.credoff:hidden').val());

    $.ajax({
        url: 'do.php',
        type: 'POST',
        data: "userID=" + onlyID,
        success: function (data) {
            if (data != "success1" && data != "success5") {
                $("#" + theid).text(data);
            } else {

                $("#thediv_" + onlyID).fadeOut("slow");
                $('#creditsBalance').fadeOut("slow");
                newbalance = parseInt($('#creditsBalance').text());

Wouldit have to be in this format?

data: "userID=" + onlyID,
"credoff=" + credoff

解决方案

...
data: {
    userId: onlyID,
    credoff: credoff
},
...

这篇关于传递JavaScript变量使用Ajax PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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