douying2243 2013-01-16 05:38
浏览 86
已采纳

如何在不提交表单的情况下在php中获取表单值?

I am using form submition in my old site.Because of SEO purpose I changed this to onClick() function,So that it will go to a clean URL but when I am using this function other form values are not getting in the php file

Below is my Old code

 <form name="frmSearchTops" id="frmSearchTops" action="/womens/tops/" method="post">
        <select  name="cboCityNameRes" id="cboCityNameRes">
          <option value=''>City</option>
             {foreach from= $arritemCities item=foo}                      
               {if $smarty.post.hdnCityName eq $foo}                                
                  <option value="{$foo}" selected="selected">{$foo}</option>          
               {else}    
                <option value="{$foo}" {if $foo eq 'City'} selected="selected"{/if}>
                     {$foo}
                </option>
               {/if}    
             {/foreach}    
        </select>
    <select name="cboMaterial" id="cboMaterial">
      <option value="">Material</option>
      <option value="CT">Cotton</option>
      <option value="SL">Silk</option>
    </select>
     <input name="btnSearch" type="button" value="Search" id="btnSearch"/>       
 </form>

new code is the below one here I am giving a dummy URL in controller action it will redirect to womens/tops.php file its redirecting but I am not getting the selected type of material

JavaScript

 <script type="text/javascript">

          function searchitem()
          {
        $strCity    =   document.getElementById('cboCityNameRes').value;
        window.location.href = "http://www.legacy.com/womens-tops-"+$strCity+".php";
          }
    </script>   

HTML Code

 <select  name="cboCityNameRes" id="cboCityNameRes" onchange="cityChanged()">
      <option value=''>City</option>                  
       {foreach from= $arritemCities item=foo}                        
          {if $smarty.post.hdnCityName eq $foo}                                 
             <option value="{$foo}" selected="selected">{$foo} </option>                      
          {else}    
             <option value="{$foo}" {if $foo eq 'City'} selected="selected"{/if}>
                 {$foo} 
            </option> 
          {/if} 
        {/foreach}    
    </select>                        
   <select name="cboMaterial" id="cboMaterial">
     <option value="">Material</option>
     <option value="CT">Cotton</option>
     <option value="SL">Silk</option>
   </select>                    
    <input name="btnSearch" type="button" value="Search" id="btnSearch" onclick="searchitem()"/>
  • 写回答

3条回答 默认 最新

  • dsbiw2911188 2013-01-16 05:51
    关注

    When you do window.location like that you're redirecting the page and this is a GET request not a POST.

    You'd need to add the extra variables to a query string e.g.

    window.location.href = "http://www.legacy.com/womens-tops-" + strCity +".php?something=blah";

    And then something would be blah in the PHP called.

    Another easier way is to use jQuery (or plain JavaScript if you prefer) to change the action of the form so that the path was your "womens-tops-$strCity" and then submit the form.

    var strCity = document.getElementById('cboCityNameRes').value;
    var frm = document.getElementById('frmSearchTops')
    frm.action = "/womens-tops-" + strCity + ".php";
    frm.submit();
    

    Or jQuery

    var strCity = $('#cboCityNameRes').val();
    $("#frmSearchTops").attr("action", "/womens-tops-" + strCity + ".php");
    $("#frmSearchTops").submit();
    

    Though search engine robots aren't going to run any JavaScript at all. So this isn't really doing anything for you with SEO. At least as far as I can see from your example. You may have some other reason to do this though so the answer still stands.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline