Solved

Wordpress Append Form Data To URL

Posted on 2013-01-23
7
473 Views
Last Modified: 2013-01-23
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.
0
Comment
Question by:NolesNYC
  • 4
  • 3
7 Comments
 

Author Comment

by:NolesNYC
ID: 38811500
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 38811580
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
 

Author Comment

by:NolesNYC
ID: 38811609
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 38811632
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
 

Author Comment

by:NolesNYC
ID: 38811688
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 38811721
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
 

Author Comment

by:NolesNYC
ID: 38811792
I have it working, thank you very much for your help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question