Link to home
Start Free TrialLog in
Avatar of NolesNYC
NolesNYC

asked on

Wordpress Append Form Data To URL

So I've created my first wordpress widget that really is nothing more than a form that request a zip code. When that form is submitted to specified page I would like to append the zip code to the url as such: www.mydomain.com/locations?address=VALUE so that the store locator plugin I'm using will filter the list according to the entered zip code... The is my code thus far, but I'm having a hard appending the value to the input field to the URL.

    echo '<div id="subpages-widget-2" class="widget widget_subpages custom-formatting">';
	echo '<h3 class="widgettitle">Find Your Clinic</h3>';
	echo '<p>Enter you zip code below to find the closest clinics near you.</p>';
	echo '<form class="searchform" method="post" action="http://www.nuvdev.com.php53-23.ord1-1.websitetestlink.com/weightlossclinics/locations-detail-list?address=$qsearch">';
    echo '<input id="qsearch" type="text" placeholder="Enter zip code...">';
    echo '<input id="submit" type="submit" value="Search">';
    echo '</form>';
	echo '</div>';

Open in new window


I'm sure this is a simple syntax issue, anyways thanks in advance.
Avatar of NolesNYC
NolesNYC

ASKER

I'll just add the actually url so you can see the actual code site
http://www.nuvdev.com.php53-23.ord1-1.websitetestlink.com/weightlossclinics/locations-detail-list/?address=33606 

That includes the variable obviously.

The widget can be seen here: http://www.nuvdev.com.php53-23.ord1-1.websitetestlink.com/why-nuviva/
Avatar of Jason C. Levine
but I'm having a hard appending the value to the input field to the URL.

You have a fundamental misunderstanding of the form operation.

echo '<form class="searchform" method="post" action="http://www.nuvdev.com.php53-23.ord1-1.websitetestlink.com/weightlossclinics/locations-detail-list?address=$qsearch">';
    echo '<input id="qsearch" type="text" placeholder="Enter zip code...">';

Open in new window


The form isn't submitted yet so how is $qsearch supposed to populate the action?  

The way forms work is the field/data values are posted to the target specified in the action.  If there is a script on the other end, it processes the data and makes the values available in variables if you specify that to be so.

In order for the above code to work the way you intend, the qsearch field would need to be posted before the form HTML renders.  You are trying to get it happen simultaneously and that won't work unless you really want to overcomplicate things and get AJAX involved.

You need to change the locations-detail-list page and add code to the page template that looks for $_POST['qsearch'] or $_REQUEST['qsearch'] and process that value and return the list.
OK- So that makes perfect sense to me, and what I suspected was the case.

So my revised question would be how (in wordpress) Can I use the $_REQUEST on the method on the locations-detail-list to append the query to the URL.

I have an idea of how to do it without Wordpress via the php header() string but not sure how to do it with wordpress.
You shouldn't need to mess with header() string or append the value to the URL to make this happen.  By POSTing the form to locations-detail-list you are putting the qsearch variable into the $_POST and $_REQUEST arrays in PHP.  

I am assuming that locations-detail-list is a custom page template that does something based on the zipcode value. If so, you just need to alter the code so that it looks for $_POST or $_REQUEST instead of the query string.  All other code remains the same.
So don't worry about the URL...

I changed my form input name to "address" bc that's being used on the detail page (see the two bits of code below) But it's still will not get the form value.

Is the first form action type Post or Get?

This is the widget form:
<form class="searchform" method="post" action="http://www.nuvdev.com.php53-23.ord1-1.websitetestlink.com/weightlossclinics/locations-detail-list/"><input id="address" placeholder="Enter zip code..." type="text"><input id="submit" value="Search" type="submit"></form>

Open in new window


This bit of code is on the detail list page:
		$display .= '<form method="GET">';
			$display .= '<input type="text" id="store_wpress_address" name="store_wpress_address" style="width:440px;" value="'.$_GET['address'].'" />';
			$display .= ' <input type="submit" id="store_wpress_search_btn" value="'.$GLOBALS['store_locator_lang']['search'].'" style="padding:2px;"/>';

Open in new window


If it helps this is the plugin im using: http://codecanyon.net/item/advanced-store-locator-for-wordpress/238166
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have it working, thank you very much for your help!