【问题标题】:PHP-AJAX Multiple Forms PostPHP-AJAX 多表单发布
【发布时间】:2014-04-04 02:51:12
【问题描述】:

在我的 PHP 应用程序的一个模块中,有多个表单,由服务器端生成的唯一 ID 和名称为“formToProcess1、formToProcess2、...、fromToProcessN”。但是,有两个 ajax 函数处理每个表单的请求,它们都以字符串形式获取表单名称作为参数,如下所示:

 function ajaxReject(formToProcess)
    {
     var ajaxRequest;
        //document.getElementById('divUploadResultAjax').style.display = 'none';



        try
        {
          ajaxRequest = new XMLHttpRequest();
        }
        catch (e)
        {
          try
          {
             ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
          }
          catch (e) 
          {
             try
             {
                ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
              }
              catch (e)
              {
                alert('Something is wrong with your browser, AJAX not working!');
               return false;
             }
          }
        }

        ajaxRequest.onreadystatechange = function()
        {
          if(ajaxRequest.readyState == 4)
          {
             var ajaxDisplay = document.getElementById('divUploadResultAjax');
             ajaxDisplay.innerHTML = ajaxRequest.responseText;
             //document.getElementById('loadingAjaxIcon').style.display = 'none';
             document.getElementById('divUploadResultAjax').style.display = 'block';
          }
        }

        var updName         = document.forms['formToProcess'].getElementById('userName').value;

                      //i'll get other elements values for query string to send if i can get this one first :)




        var queryString = '?name=' + updName + '&lastname=' + updLastname + '&bio=' + updBio + '&country=' + updCountry + '&cams=' + updCams + '&fb=' + updFacebook + '&twitter=' + updTwitter + '&processRequest=' + RequestProcess;

        alert (queryString);
        return;


        ajaxRequest.open('GET', 'dashboardUpdateProfile.php' + queryString, true);
        ajaxRequest.send(null); 
                        }



#Every form in the php page is generated like following:

        echo '

                                        <div id="divUploadResultAjax">
                                            <form name="profileUpdater'. $formCounter .'">
                                                <input type="hidden" id="uluid" name="uluid" value="'.$UL_UploadID.'" />
                                                <table class="tg" style="width: 100%">
                                                    <tr>
                                                        <td style="font-weight: bold; width: 250px; color: #000000;" colspan="3">
                                                            <h3>Upload ID #'.$UL_UploadID.' </h3> (Uploaded On '.$NewCreateDate.')
                                                        </td>
    ...
    ...
    bla bla bla
    ...
    ...

    ----> this is where i call functions in each form.
    <input type="button" class="btnRegister" name="updateProfile"     onclick="ajaxReject(\'profileUpdater'. $formCounter .'\')" value="Reject" />
             <input type="button" class="btnRegister" name="updateProfile" onclick="ajaxApprove()"     value="Approve" />

我不太确定出了什么问题。当我通过单击按钮调用该函数时,它可以完美运行,直到 if(ajaxRequest.readyState == 4) 结束,之后,当我尝试获取 var updName = document.forms['formToProcess'].getElementById( '用户名').value;功能崩溃。我做错了什么?

谢谢。

【问题讨论】:

    标签: php jquery ajax arguments


    【解决方案1】:

    你不能用var updName = document.getElementById('userName').value;之类的东西吗?

    我没有在表单中找到用户名元素?它在你的实际代码中吗?

    【讨论】:

    • 是的,我的表单中有一个名为 userName (input) 的元素。我可以通过简单地执行我上面提到的那个来获得它的价值,但是当它也以其他形式多次包含相同的名称时,它只会获得第一个,而不是其他形式。这就是为什么我需要做类似的事情: document.forms[formName].getElementById('userName').value;但它不起作用,我被困在这里:(
    • 为什么不用var updName = document.forms[formToProcess].userName.value;这样的元素名称
    • 您是否注意到我没有使用 getElementById 并且没有在 formToProcess 周围使用 '(引号)?
    • 好的,它就像魅力一样,我有语法错误。非常感谢!
    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多