尝试获取URL参数,并使用表单POST它们

时间:2014-10-04 01:41:16

标签: javascript forms

我正在尝试获取着陆页抓取网址参数。我在表单中有两个与参数名称匹配的隐藏字段,但我似乎无法使其工作。

我需要获取URL参数,然后将其与表单的其余部分一起发送。表单的方法=" POST">

我需要使用JavaScript,而不是PHP。

谢谢!

例如,URL将如下所示:

http://mywebsite.com/qualify/new/?AFFILIATE_ID=-1&SUB_1=Test#

这是我的表单代码:

 <html>
  <head>
     <script>
function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
</script>

                  请选择保险类型                    私人/团体保险                    医疗保险/医疗补助                    二级保险                         

 <input type="text" name="phone" placeholder="Phone Number" required="required">
 <select value="State" name="state" style="width:100%;height: 35px!important; margin-top: 10px!important;">
             <option value="">Please Choose a State:</option>
               <option value="Alabama">Alabama</option>
               <option value="Arizona">Arizona</option>      
               <option value="California">California</option>         
               <option value="Colorado">Colorado</option>       
               <option value="Connecticut">Connecticut</option>          
               <option value="Delaware">Delaware</option>       
               <option value="District Of Columbia">District Of Columbia</option>                   
               <option value="Florida">Florida</option>      
               <option value="Georgia">Georgia</option>      
               <option value="Illinois">Illinois</option>       
               <option value="Indiana">Indiana</option>      
               <option value="Iowa">Iowa</option>
               <option value="Kentucky">Kentucky</option>       
               <option value="Maine">Maine</option>    
               <option value="Maryland">Maryland</option>       
               <option value="Massachusetts">Massachusetts</option>            
               <option value="Michigan">Michigan</option>       
               <option value="Minnesota">Minnesota</option>        
               <option value="Missouri">Missouri</option>       
               <option value="Montana">Montana</option>      
               <option value="Nebraska">Nebraska</option>       
               <option value="New Hampshire">New Hampshire</option>            
               <option value="New Jersey">New Jersey</option>         
               <option value="New Mexico">New Mexico</option>         
               <option value="New York">New York</option>       
               <option value="North Carolina">North Carolina</option>             
               <option value="North Dakota">North Dakota</option>           
               <option value="Ohio">Ohio</option>   
               <option value="Oklahoma">Oklahoma</option>       
               <option value="Pennsylvania">Pennsylvania</option>           
               <option value="Rhode Island">Rhode Island</option>           
               <option value="South Carolina">South Carolina</option>             
               <option value="Tennessee">Tennessee</option>        
               <option value="Texas">Texas</option>    
               <option value="Utah">Utah</option>   
               <option value="Virginia">Virginia</option>       
               <option value="Washington">Washington</option>         
               <option value="West Virginia">West Virginia</option>            
               <option value="Wisconsin">Wisconsin</option>                                          
               </select>            
               <input type="number" min="18" max="64" step="1" name="birthdate" placeholder="Age" required="required" style="margin-bottom: 8px!important;">
 <input type="text" name="insuranceComp" placeholder="Insurance Company" required="required">
 <input type="text" name="memberID" placeholder="Member ID or Group Policy" required="required">
 <input type="text" name="RxBin" placeholder="Rx Bin Number" required="required">
 <input type="text" name="PcnNumber" placeholder="PCN Number" required="required">
 <input type="text" name="groupID" placeholder="Rx Group ID" required="required">
 <input type="hidden" name="SUB_1" value="">
 <input type="hidden" name="AFFILIATE_ID" value="">
 <button class="submit" style="width: 100%;">Submit Now</button>
</form>
   </body>
</html>

3 个答案:

答案 0 :(得分:0)

如果您的意思是GET值,您可以通过在适当的位置回显整个表单并使用变量来完成此操作;

<?php
echo'
 <input type="text" name="phone" placeholder="Phone Number" required="required">
 <select value="State" name="state" style="width:100%;height: 35px!important; margin-top: 10px!important;">
             <option value="">Please Choose a State:</option>
               <option value="Alabama">Alabama</option>
               <option value="Arizona">Arizona</option>      
               <option value="California">California</option>         
               <option value="Colorado">Colorado</option>       
               <option value="Connecticut">Connecticut</option>          
               <option value="Delaware">Delaware</option>       
               <option value="District Of Columbia">District Of Columbia</option>                   
               <option value="Florida">Florida</option>      
               <option value="Georgia">Georgia</option>      
               <option value="Illinois">Illinois</option>       
               <option value="Indiana">Indiana</option>      
               <option value="Iowa">Iowa</option>
               <option value="Kentucky">Kentucky</option>       
               <option value="Maine">Maine</option>    
               <option value="Maryland">Maryland</option>       
               <option value="Massachusetts">Massachusetts</option>            
               <option value="Michigan">Michigan</option>       
               <option value="Minnesota">Minnesota</option>        
               <option value="Missouri">Missouri</option>       
               <option value="Montana">Montana</option>      
               <option value="Nebraska">Nebraska</option>       
               <option value="New Hampshire">New Hampshire</option>            
               <option value="New Jersey">New Jersey</option>         
               <option value="New Mexico">New Mexico</option>         
               <option value="New York">New York</option>       
               <option value="North Carolina">North Carolina</option>             
               <option value="North Dakota">North Dakota</option>           
               <option value="Ohio">Ohio</option>   
               <option value="Oklahoma">Oklahoma</option>       
               <option value="Pennsylvania">Pennsylvania</option>           
               <option value="Rhode Island">Rhode Island</option>           
               <option value="South Carolina">South Carolina</option>             
               <option value="Tennessee">Tennessee</option>        
               <option value="Texas">Texas</option>    
               <option value="Utah">Utah</option>   
               <option value="Virginia">Virginia</option>       
               <option value="Washington">Washington</option>         
               <option value="West Virginia">West Virginia</option>            
               <option value="Wisconsin">Wisconsin</option>                                          
               </select>            
               <input type="number" min="18" max="64" step="1" name="birthdate" placeholder="Age" required="required" style="margin-bottom: 8px!important;">
 <input type="text" name="insuranceComp" placeholder="Insurance Company" required="required">
 <input type="text" name="memberID" placeholder="Member ID or Group Policy" required="required">
 <input type="text" name="RxBin" placeholder="Rx Bin Number" required="required">
 <input type="text" name="PcnNumber" placeholder="PCN Number" required="required">
 <input type="text" name="groupID" placeholder="Rx Group ID" required="required">
 <input type="hidden" name="SUB_1" value="'.$_GET['value1'].'">
 <input type="hidden" name="AFFILIATE_ID" value="'.$_GET['value2'].'">
 <button class="submit" style="width: 100%;">Submit Now</button>
</form>
';
?>

或者你可以只回显隐藏的字段,如此;

<?php
echo'
     <input type="hidden" name="SUB_1" value="'.$_GET['value1'].'">
     <input type="hidden" name="AFFILIATE_ID" value="'.$_GET['value2'].'">
';
?>

答案 1 :(得分:0)

您的概念方法似乎合理合理,只是一些实施问题。代码可能如下所示:

$(document).ready(function () {
 if(window.location.search && window.location.search.indexOf('&') !== -1) {
   var pairs = window.location.search.replace('?', '').split('&');
   $.each(pairs, function (index, pair) {
     var parameter = pair.split('=')[0];
     var value = pair.split('=')[1];

     if(parameter === 'AFFILIATE_ID' || parameter === 'SUB_1') {
       $('input[name="' + parameter + '"]').val(value);
     }

   });
 }
});

注意:这个答案使用jQuery,在这种情况下可能最适合浏览器兼容性,但如果你需要,我可以发布纯JS解决方案。

答案 2 :(得分:0)

您的代码有效。您只需添加填充表单字段的部分:

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);

    var returnval = match && decodeURIComponent(match[1].replace(/\+/g, ' '));

    // populate form fields
    $("[name='"+name+"']").val( returnval );

    return returnval;
}

// fire your function when the form submits
$(function(){
    $('#contact-form').on('submit',function(){
        getParameterByName('AFFILIATE_ID');
        getParameterByName('SUB_1');
    });
});