从函数Javascript设置变量PHP的值

时间:2017-07-15 20:19:52

标签: javascript php

我有一个表单,我的客户必须在输入处输入一些值。

我的表格是这样的:

<form action="javascript:send();" enctype="multipart/form-data" id="form_main"><input type="number" name="quantidade" id="qt">

我有一个名为“send()”的函数。

function send(arg1){


    if(typeof(arg1) === "undefined"){
        arg1 = 2
    }



    document.getElementById('qt').setAttribute('value',arg1);

下面我有一些ajax帖等等......

我的所有表单都是通过方法发送到此页面:pacific_cbc_pr.php

我有这样的变量。

$quantidade = $_POST['quantidade'];
问题是当有人例如在输入量化时输入100或200。

变量$ quantidade必须为“2”,因为我没有传递参数。但是,$ quantidade设置为客户输入的值。

有人可以帮助我,因为变量$ quantidade必须是以下值:

document.getelementbyID ....

编辑:函数发送整个。

function send(arg1){
    if(typeof(arg1) === "undefined"){
        arg1 = 2
    }
    document.getElementById('qt').value = arg1;

        $("#warning_alerta").hide();
        $("#input_vazio").hide();
        $("#login_erro").hide();


        var formData = new FormData($('#form_principal')[0]);
        $("#loading").show();
        setTimeout(function() {
            $.ajax({
                url: "/xxxx/xxxx/xxxx/pacific_cbc_pr.php",
                type: "post",
                data: formData,
                cache: false,
                async:true,
                contentType: false,
                processData: false,
                success: function(result){
                    if(result == 1)//logado
                    {
                        //$("#hide").show();
                        $('#text_modal').html('que');
                        $('#modal-container-188641').modal('show');
                    }
                    else if (result == 200)
                    {

                      $("#login_erro").show();

                    }
                    else if (result == 300)
                    {
                        $("#login_sucesso").show();
                        setTimeout(function(){
                            window.location = "index.php";},3000);
                    }
                    $("#loading").hide();
                }
            })

        },300);
};

感谢。

1 个答案:

答案 0 :(得分:2)

你需要像下面这样做: -

function send(){
    document.getElementById("qt").value = 2;
}

BTW而不是这么做,你可以简单地将自定义数据传递到你的ajax帖子中。

如下所示: -

function send(){

    $('#qt').val(2);//this is what you have to do

    $("#warning_alerta").hide();
    $("#input_vazio").hide();
    $("#login_erro").hide();
    $("#loading").show();
    setTimeout(function() {
        $.ajax({
            url: "/xxxx/xxxx/xxxx/pacific_cbc_pr.php",
            type: "post",
            data: $('#form_main').serializeArray(),
            cache: false,
            async:true,
            contentType: false,
            processData: false,
            success: function(result){
                if(result == 1)//logado
                {
                    //$("#hide").show();
                    $('#text_modal').html('que');
                    $('#modal-container-188641').modal('show');
                }else if (result == 200){
                    $("#login_erro").show();
                }else if (result == 300){
                    $("#login_sucesso").show();
                    setTimeout(function(){
                        window.location = "index.php";},3000);

                }
                $("#loading").hide();
            }
        });

    },300);
};
相关问题