Solved

Wordpress Append Form Data To URL

Posted on 2013-01-23
7
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
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…

737 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