GET request without the question mark - php

I am a customer service assistant and I wanted to make a simple form to check the price of products on the webpage, without having to load the whole homepage.
The website is www.homebase.co.uk, and the search URL is http://www.homebase.co.uk/en/homebaseuk/searchterm/.
I want to make a form where it will add the text entered in the form after /searchterm/ without the question mark.
EG if I type in 775199 and press submit/search it will navigate to http://www.homebase.co.uk/en/homebaseuk/searchterm/775199.
Thanks so much for your help all :)
I really appreciate it!

Assuming you are using PHP. I think what you want to do is this:
<?php
//THIS IS THE SAME PAGE WHERE YOUR SEARCH FORM EXISTS
$searchPage = "http://www.homebase.co.uk/en/homebaseuk/searchterm/";
$formAction = "./"; //FORM IS SUBMITTED BACK TO ITSELF...
if(isset($_POST['search_term']){
//BUILD THE REDIRECTION LINK BASED ON THE DEFAULT SEARCH PAGE: $searchPage . $_POST['search_term'])
//AND THEN REDIRECT TO THE REDIRECTION LINK
header("location: " . $searchPage . htmlspecialchars(trim($_POST['search_term'])) );
exit;
}
Your HTML Form might look something like this:
<form method="POST" action="<?php echo $formAction; ?>" >
<input type="text" name="search_term" id="search_term" value="" class="search_term" />
<input type="submit" name"submit" id="submit" class="submit" value="Search" />
</form>

Okay, so I solved the problem:
<style type="text/css">
.biga {
font-family: Arial, Helvetica, sans-serif;
font-size: 36px;
color: #F90;
background-color: #FFF;
border: medium solid #000;
}
}
.centerpoint {
text-align: center;
}
</style>
<title>Product Search</title>
<p>
<div class="centerpoint"> <span class="centerpoint">
<input name="prog_site" type="text" class="biga" id="prog_site" value="" />
<a href="http://"
onclick="this.href=(
'http://www.homebase.co.uk/en/homebaseuk/searchterm/' + document.getElementById('prog_site').value)"
target="_blank">
<input name="so_link" type="button" class="biga" value="Search Product">
</a>
</p>
</span></div>
This is the code I used. Thanks for all of your help!

Related

input value on php form between pages

I need you help. I have a form, and 2 inputs inside.
I have also those php code.
The idea is change css elements, by 2 colors the user insert, when my url is "URL?MyColors=true",
The function MyColors(), will run.
And then, the css apply by the inputs values.
I also want when client is submit, the url with get Get parameter "Mycolors=true",
so that why the submit button is with the next "formaction":
"".
It's work as well, but only for current page, and "URL2?MyColors=true".
When I'm refreshing the page, the 2 colors not apply anymore.
I don't know what the reason.
Who can help me?
<form action="" method="post" id="myform">
myColor1: <input name="myColor1" type="text" />
myColor2: <input name="myColor2" type="text" />
<input formaction="index.php?MyColors=true" type="submit" name="submit">
<button class="refresh" onclick="refreshPage()">Refresh</button>
</form>
<?php
if(isset($_GET['MyColors'])){
MyColors();
}
function MyColors() {
$_SESSION["myColors"] = "&MyColors=true";
if (isset($_POST['submit'])) {
$myColor1 = $_POST['myColor1'];
$myColor2 = $_POST['myColor2'];
}
else {
$myColor1 = $_SESSION['myColor1'];
$myColor2 = $_SESSION['myColor2'];
}
$_SESSION['myColor1'] = $_POST['myColor1'];
$_SESSION['myColor2'] = $_POST['myColor2'];
echo "<style>.bgimg-1{background: {$myColor1}!important;}
.last{color:{$myColor1}!important;-webkit-text-stroke-color:{$myColor2}!important;}
.first{color:{$myColor2}!important;-webkit-text-stroke-color:{$myColor1}!important;}
.addToCart{background:{$myColor1}!important;color:{$myColor2}!important;}
.addToCart i, a.addToCart a , a.addToCart span {color:{$myColor2}!important;}
.moreInfo{color: {$myColor1}!important;background: {$myColor2}!important;}
a.moreInfo i{color: {$myColor1}!important;}
button[type='button'].view_data {color: {$myColor1}!important;background: {$myColor2}!important;}
p.rotate {color: {$myColor1}!important; }
body {background: {$myColor2}!important;}
.categoriesTitle {color: {$myColor1}!important;}
.overlay {background: {$myColor1}!important;}
a.liAfter {color: {$myColor2}!important;}
.categoryTitles {color: {$myColor2}!important;background: {$myColor1}!important;}
.catWrapperImageCatPage::after {background: {$myColor2}!important;}
p.cat_title {color: {$myColor1}!important; }
</style>";
}
?>
your code should be...
<form action="index.php?MyColors=true" method="post" id="myform">
<label for="mycol1">myColor1:</label>
<input name="myColor1" id="mycol1" type="text">
<label for="mycol2">myColor2:</label>
<input name="myColor2" id="mycol2" type="text">
<input type="submit" name="col-submit" value="Submit_Color">
<button class="refresh" onclick="refreshPage()">Refresh</button>
</form>
<?php
if(isset($_POST["col-submit"])){
session_start();
----------
bla bla...
----------
?>

How to add php to wordpress form

I'm having a very hard time trying to get a php code integrated into a form I've created on wordpress. I've trying 3 different php plugins to no avail. Here is the html and css. Does anyone have any idea where I should be putting the php on wordpress and how I should properly call it through the "action" in the form?
css:
<style>
input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%;
border: 1px solid #E5E5E5; padding: 10px 20px;}
input[name=ffirstname] {width:49%; margin-right:1%; }
input[name=lastname] {width:49%; margin-left:1%; }
input[name=address] {width:65.6667%; margin-right:1%; }
input[name=unit] {width:32.3337%; margin-left:1%; }
input[name=city] {width:49%; margin-right:1%; }
select[name=province] {width:24%; margin-left:1%;}
input[name=postal] {width:24%; margin-left:1%; }
input[name=email] {width:49%; margin-right:1%; }
input[name=phone] {width:49%; margin-left:1%;}
input[class=submit] {
background-color: #f05a28;
color: white;
padding: 8px 20px;
margin-left: 85%;
border-radius: 4px;
border: none; !important;
outline: none; !important ;
cursor: pointer;
box-shadow: none; !important;
}
</style>
html:
<form name"infoform" method="post" action="form-to-email.php" >
<br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/>
<br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br>
<br><input type="text" name="city" placeholder="City"/><select name="province" form="form1">
<option value="ab">AB</option>
<option value="BC">BC</option>
<option value="BC">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="PE">PE</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select><input type="text" name="postal" placeholder="Postal Code"/></br>
<br><input type="text" name="country" placeholder="Country"/></br>
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br>
<br> <br><input type="submit" value="Next"/>
php:
<?php
$name = $_POST['ffirstname'];
$email = $_POST['lastname'];
$message = $_POST['address'];
$to = "joeblow#hotmail.com";
$subject = "My contact form";
$body = " You have received a new";
mail($to,$subject,$body);
?>
Usually I use footer.php to add JS and PHP code, just before the closing tag of the body. But if that does not work try using header.php. Maybe your code needs to be included before the end.
The HTML part you can put inside a page or post without a problem.
If you want to use a plug-in, you can easily do that with Contact form 7.
Check here and here for tutorials.
UPDATE - to add PHP code into Wordpress:
You can add the code between <?php tags, so open your header.php, footer.php or page.php and write your code inside the tag
<?php
//PHP Code here
?>
This way you will see this code in every Wordpress page on your site.
Header and footer are always included on all pages.
If you only need it in post or page use page.php or post.php
If you want control over the placement of php within a Wordpress page then have a look at page templates. You could create a form page template that had the form and the necessary php to deal with it, and then apply that to a given url e.g. /sign-up. That way you wouldn't have to have the form code cluttering up the header.php or footer.php of every page.
With the form itself, you could just have it post to itself, and then check to see whether the form variables are set when the page is loaded using isset($_POST['field_name']). If they're present then you can assume the form was posted, and you need to therefore send the email, otherwise if they're not present then you can assume you need to display the form.
e.g.
<?php if(isset($_POST['ffirstname'])){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields, and then send email
// could drop out of php here and show a thank you message
?>
<p>Thank you for submitting the form</p>
<?php
} else {
// $_POST['ffirstname'] not set, so show form ?>
<form method="post" action="">
<input type="text" name="ffirstname" />
<input type="submit" value="Submit" />
</form>
<?php } ?>
If you want to hook into Wordpress for this, then one way to do it is to add a hidden input called action and set its value to the name of a function in your functions.php file. You'll also need to add some javascript to make an ajax submission to the function that could then do the logic and email sending:
e.g.
// Form Page
<form method="post" action="" id="emailForm">
<input type="text" name="ffirstname" />
<input type="hidden" name="action" value="sendEmail" />
<input type="submit" value="Submit" />
</form>
// Somewhere further down the same page as the form
<script type="text/javascript">
jQuery('#emailForm').on('submit', function() {
e.preventDefault();
jQuery.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: jQuery('#emailForm').serialize(),
success: function(data) {
// Perhaps some UI update here to let the user know
// the submission was successful
},
error: function(error) {
// Do something with the error
}
});
});
</script>
Then in your themes functions.php file:
// Functions.php
function sendEmail(){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields as necessary, and then send email
return wp_mail($recipient, $subject, $body, $headers);
}
add_action('wp_ajax_sendEmail', 'sendEmail');

Trouble with form validation using PHP

I have a relatively simple sounding question that hopefully entails a simple answer. I am currently developing a website that is a scroll-down type; everything is on one page, you know the one. My 'contact' section is at the very bottom of the page, and I am of course doing form validation with PHP. The validation part works, I have no trouble with that. However, if validation fails, the browser takes me back all the way back to the top of the page; this is inconvenient for obvious reasons.
I am aware of the 'header()' function, which I can use to keep me at the bottom of the page if an error occurs. As an example, at the top of my HTML page before the DOCTYPE, I can write:
if ($errors) {
header(Location: 'somelocation.php#contact');
}
This works, but for some reason it prevents my PHP embedded in my html to work. If some errors occur, I want to display them using PHP on the page:
<h2>Contact</h2>
<?php if($errors) { ?>
<p class="warning">Please fix the errors</p>
<?php } ?>
'Please fix the errors' does not appear. It does appear however, if I remove the header function from the page, so I know the problem is related. So basically, if I remove the header() function, the page goes back to the top, and the errors show; if I keep the header() function, the page correctly stays where it is, but no errors show.
Alternatively, this question also can be asked in the case of, what happens when the form is validated correctly, the 'header' function is called, and I want to stay at the bottom of the page and display some HTML saying 'Thanks, your form has been submitted'? (I don't want to go to a 'thank-you' page or anything, just stay in the same spot and give a 'thanks' on the page) I assume I'd run into the same problem. Is there a solution to this?
Thanks in advance!
Specifying a location in the header will cause the browser to redirect to that URI. This is being called before any output is sent to the browser.
When the page is reset, $errors == false, so the paragraph isn't displayed.
It sounds like you need to integrate AJAX, or a mixture of server- and client-side code here.
A very simple and efficient way to validation of a form is using jquery, A perfect example of this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<em>*</em><input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<em> </em><input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
</body>
</html>
Happy Coding.!!!

Invisible CAPTCHA not working, most likely incorrect PHP?

I am trying to incorporate an invisible CAPTCHA into my contact form, however I am not sure if it works.
My code is as follows...
HTML
<span class="test-field"><label for="test">Please leave blank</label>
<input type="text" name="test" id="test"/></span>
CSS
.test-field {Display: none}
PHP
<?php
if($_POST["test"]!=""){
header("Location: {$_SERVER[HTTP_REFERER]}");exit;
}
// Get Data
$f_name = strip_tags($_POST['f_name']);
$f_email = strip_tags($_POST['f_email']);
$f_message = strip_tags($_POST['f_msg']);
// Send Message
mail( "me#website.com", "Website Contact",
"Name: $f_name\nEmail: $f_email\nMessage: $f_message\n",
"From: My Website" );
?>
To test it, I did a few tests changing the...
f($_POST["test"]!="")
...to...
f($_POST["test"]="")
My PHP is not very good, but I am guessing this should not send the form through if the field is blank, but it does come through. Is there anything wrong with my PHP?
Thanks in advance.
As far as your specific question it doesn't make any sense. A user CAPTCHA is absolutely useless if it's hidden.
The term CAPTCHA stands for ...
Completely Automatic Public Turing Test to Tell Computers and Humans Apart
Notice it says humans ...
How can we test for human interaction if they can't see the CAPTCHA challenge? You Can't. Never-the-less .... here's a quick mock up of your code ... the CAPTCHA I left out since it make no sense. Take it from here ...
<?php
if ($_POST['test'] == "") {
header("Location: {$_SERVER['HTTP_REFERER']}");
} else {
// Get Data
$f_name = strip_tags($_POST['f_name']);
$f_email = strip_tags($_POST['f_email']);
$f_message = strip_tags($_POST['f_msg']);
}
?>
<html>
<head>
<style type="text/css">
#demo {
width: 250px;
height: 250px;
}
.test-field {
display: none;
width: 200px;
height: 200px;
}
#form label {
width: 50px;
float: left;
}
#form input {
width: 150px;
float: right;
}
</style>
</head>
<body>
<div id="demo">
<form name="form" id="form" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label for="test">Empty </label><input type="text" name="test" id="test"/><br/>
<label for="f_name">Name</label><input type="text" name="f_name" id="f_name"/><br/>
<label for="f_email">Email</label><input type="text" name="f_email" id="f_email"/><br/>
<label for="f_msg">Message</label><textarea rows="15" cols="30" name="f_msg" id="f_msg"></textarea>
<input type="submit" id="submit">
<div class="test-field"></div>
</form>
</div>
</body>
</html>
<?php var_dump($_POST) ?>
Try changing
if($_POST["test"]!=""){
to
if(isset($_POST["test"]) && $_POST["test"]){

Wordpress plugin $post

I have already get how to insert in the database. My only problem is to get the value of text box to insert in the database. Here is my code:
function dbAdd() {
global $wpdb;
$new_title = $_POST['new_title'];
$new_author = $_POST['new_author'];
$new_url = $_POST['new_url'];
if($wpdb->insert('wp_podcast_data', array( 'mp3_id' => '', 'title' => '$new_title', 'creator' => '$new_author', 'url' => '$new_url') ))
{
echo"<h1>Save Successfully!</h1>";
}else
{
echo mysql_error();
}
}
----------------------------------------------------------
function player_manager_index() {
if($_SERVER['REQUEST_METHOD']=='POST')
{
dbAdd();
}
?>
<h3>Podcast Player Manager (This Plug is not yet finish)</h3><br />
<p>Note: This Player Manager needs the URL of mp3 file that you want to include in your podcast player.</p>
<form method="post" action="">
<label for="new_title" style="display:block; padding-top: 5px; cursor: default;">Title</label><input type="text" id="new_title" name="new_title" size="50" />
<label for="new_author" style="display:block; padding-top: 5px; cursor: default;">Author</label><input type="text" id="new_author" name="new_author" size="50" />
<label for="new_url" style="display:block; padding-top: 5px; cursor: default;">URL</label><input type="text" id="new_url" name="new_url" size="50" />
<div><input type="submit" value="Add New" style="margin-left: 20px; margin-top: 15px;" /></div>
</form>
Please Help me. I'm newbie in wordpress. Thank you so much.
Correct me if I am wrong someone, but I could have sworn that Wordpress merges both the $_POST and $_GET variables into the one $_REQUEST variable. So if you replace all of your $_POST's with $_REQUEST instead you might find it will work.
In your player_manager_index function I wouldn't be using the following: if($_SERVER['REQUEST_METHOD']=='POST')
Replace that with:
if($_REQUEST['new_title'])
So you are instead checking if a variable is being sent, as opposed to just permitting the function to run if it posted to.

Categories