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.
NolesNYCAsked:
Who is Participating?
 
Jason C. LevineConnect With a Mentor No oneCommented:
A couple of issues.

The form method is post.  See here:

<form class="searchform" method="post"

Open in new window


------------------------

The input field always, always takes the name parameter:

<input id="address" placeholder="Enter zip code..." type="text">

Open in new window


No name parameter above, so you're going to have issues trying to pull the data back out.  Try this:

<input id="address" name="address" placeholder="Enter zip code..." type="text">

Open in new window


------------------------

So from the above, the following:

value="'.$_GET['address'].'" />';

Open in new window


should be

value="'.$_POST['address'].'" />';

Open in new window


or

value="'.$_REQUEST['address'].'" />';

Open in new window


If it helps this is the plugin im using: http://codecanyon.net/item/advanced-store-locator-for-wordpress/238166

It doesn't help, it may actually be complicating matters.  Are you sure the plugin supports passing a previous form value this way? Generally speaking, a well-coded plugin will trash or hopefully at least sanitize inputs to prevent injection attacks.
0
 
NolesNYCAuthor Commented:
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/
0
 
Jason C. LevineNo oneCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
NolesNYCAuthor Commented:
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.
0
 
Jason C. LevineNo oneCommented:
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.
0
 
NolesNYCAuthor Commented:
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
0
 
NolesNYCAuthor Commented:
I have it working, thank you very much for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.