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

如何在不提交表单的情况下在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()"/>

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.

You have a select with the onchange event calling the function cityChanged yet you only posted the function searchitem.

Nevertheless, you can pass the value selected for a select doing something like:

<select  name="cboCityNameRes" id="cboCityNameRes" onchange="cityChanged(this.value)">
...
</select>

then using the value of the function argument with something like:

function cityChanged(city){
    var material = document.getElementById("cboMaterial").options[document.getElementById("cboMaterial").selectedIndex].value;
    // window.location.href = "http://www.legacy.com/womens-tops-"+city+".php"
    // add material to the URL somewhere?
}

Try this

function cityChanged(val) {
var value = val.options[val.selectedIndex].value;  
window.location.href = "http://www.legacy.com/womens-tops-"+value+".php";
}

and your HTML should be like this,

<select  name="cboCityNameRes" id="cboCityNameRes" onchange="cityChanged(this)">

hope this works for you