Solved

Wordpress Append Form Data To URL

Posted on 2013-01-23
7
464 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now