I've set up a search box on the homepage, that should link into the Opencart search by passing a URL.
The problem I'm having is that the resulting URL is always unencoded:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
What I'm aiming for is
store/index.php?route=product/search&filter_name=test
My current code is:
<?php
$storesearch = $_REQUEST['store-search'] ;
$filter = 'route=product/search&filter_name';
$filtered = html_entity_decode($filter);
?>
<form id="store" method="GET" action="http://localhost/vinmobile/store/index.php?<?php echo $storesearch; ?>">
<input type="text" id="store-search" name="<?php echo $filtered; ?>" value="Store Search" onclick="this.value = '';">
<input type="submit" name="" value="Search">
</form>
<?php echo $filtered; ?> // Just to show it onscreen to make sure it's written correctly
I've tried encode, decode, html_entity_decode, urldecode, rawurldecode...
I've tried the form method as GET, POST
I've tried the variable declaration as $_REQUEST, $_GET, $_POST
Every time, I still end up with the url looking like:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
I'm sure it's something simple I've missed, but I 'think' I've tried everything and would be very grateful if someone could point me in the right direction.
Cheers
webecho
Ended up using Javascript instead and it works perfectly:
`<script>
function process()
{
var url="store/index.php?route=product/search&filter_name=" + document.getElementById("filter").value;
location.href=url;
return false;
}
</script>
<form id="store" onSubmit="return process();">
<input type="text" name="filter" id="filter" value="Store Search" onclick="this.value = '';">
<input type="submit" value="Search">
</form>`
Thanks for your suggestions though
Related
I have a question. My question title doesn't really describe what I need help with, but it's the same concept. This is what I want to do:
I am creating a search function (not open for other suggestions than the one I am trying to accomplish), and it's using query string. If I search for "Hello", I want my website to redirect to website.com?s=hello, but if there already is one, add it to the url with a &, like this: website.com?page=home&s=hello. I tried to use this:
if(isset($_GET)) { // Add the string with & before
else { // Add the string with a ? before
I am willing to use jQuery if I have to
More brief explanation:
I have a form, it has an input and a submit-button.
<form action="" method="post">
<li><input type="text" placeholder="Username"></li>
<li><input type="submit" value="Search"></li>
</form>
I am aware how to store everything in variables and such, but I am not sure how to add it as a query string to the url. Thank you!
A way to do it is to handle it in php in your form:
<?php
if(isset($_GET['page'])) {
$url = '?page=' . $_GET['page'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>`
Note that if you want to submit your form AND go to the page ?s=foo as a result of submission, you must have a method GET on your form and have an input with argument name="s".
EDIT: Or a bit more exhaustive, if you have several arg that could be passed in the URL it could be :
<?php
if(!empty($_GET)) {
$url = '?' . $_SERVER['QUERY_STRING'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>
The following is one of my HTML pages.
<form id="regForm" method="post" action="enquiry_process.php" novalidate="novalidate">
<fieldset>
<legend>Personal Details</legend>
<label>First Name:</label>
<input type="text" name="owner" id="owner" /><br />
<label>Last Name:</label>
<input type="text" name="owner2" id="owner2" /><br />
</fieldset>
<p>
<input type="Submit" onclick="validateForm()"/>
<input type="Reset" value="Reset" />
</p>
</form>
The following is my 2nd HTML page.
<form id="bookForm" method="post" action="view_enquiry.php">
<?php
$fname = $_POST['owner'];
$lname = $_POST['owner2'];
?>
<input type="hidden" name="owner" value="<?php echo $fname; ?>">
<input type="hidden" name="owner2" value="<?php echo $lname; ?>">
<fieldset>
<legend>User Details</legend>
<p>Your First Name: <span id="confirm_fname"></span></p>
<p>Your Last Name: <span id="confirm_lname"></span></p>
<input type="submit" name="submit" value="Confirm Booking" />
<input type="button" value="Cancel" id="cancelButton" onclick="cancelBooking()" />
</fieldset>
The functions you see like validateForm() and cancelBooking() are Javascript functions that validate my form or return the user from the 2nd page to the 1st and I believe they have nothing to do with my question.
When I click submit on the first HTML page, it should pass on the value of the owner and owner2 to the 2nd page right?
I keep on getting Undefined index and after looking around, it seems like I have to use isset() or empty() in my PHP, but this seems to only mask my notices but does not actually fix it? When I just add isset(), it ends up giving my 3rd page Undefined Variable. The method on my forms are already post.
Is there another problem here? Thank you.
EDIT: The following is are my relevant Javascripts.
ValidateForm:
function validateForm(){
"use strict";
gErrorMsg = "";
var nameOK = chkOwnerName();
var nameOK2 = chkOwnerName2();
var isAllOK = (nameOK && nameOK2);
if(isAllOK){
isAllOK = storeBooking();
}
else{
alert(gErrorMsg);
gErrorMsg = "";
}
return isAllOK;
}
Storebooking:
function storeBooking() {
"use strict";
sessionStorage.firstname = document.getElementById("owner").value;
sessionStorage.lastname = document.getElementById("owner2").value;
window.location = "enquiry_process.php";
}
I have another function called getbooking that runs with the condition window.onload
function getBooking(){
//if sessionStorage for username is not empty
if((sessionStorage.firstname != undefined)){
//confirmation text
document.getElementById("confirm_fname").textContent = sessionStorage.firstname;
document.getElementById("confirm_lname").textContent = sessionStorage.lastname;
}
chkOwnerName and chkOwnerName2 are functions that validate the form with patterns and I don't think they're relevant.
I also updated my 2nd HTML page with Javascript related contents because I assumed it wasn't relevant at first.
You can debug with below code by adding it in your 2nd form page.
echo "<pre>"; print_r($_POST); die;
If your form data is not going to your 2nd form then Array() will come as empty.
u can try by print_r($_REQUEST[]); on second form top page (enquiry_process.php) , i hope the both the form is in same folder and name of second form page is "enquiry_process.php" .
Since u r sending data using post form u should be able to retrieve it by print_r($_POST); or print_r($_REQUEST);
"if(empty($var))" and "if(isset($var))" are conditions, they check something and execute code within "{}" if the test returns true. So they don't 'fix' problems.
Your script worked fine for me without your js. Maybe the problem.
Just try your code step by step. You will find what's wrong.
I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .
My question is this:
How do I use a php function with an argument FROM MY php/html form?
I downloaded this php function script from intechgrity.
Link: http://www.intechgrity.com/?p=808
The function is: itg_fetch_image('http://the.image.url/pic.bmp')
What I'm doing:
1) On my website I have a php page.
2) What I did was copy all of the intechgrity php script and pasted it into my page (at the top, of course)
In my page I have this form, but nothing is happening.
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" id="Stuff" name="Stuff" maxlength="30" value="<?=$img_url;?>" />
<button type="button" onClick="newSrc();">Do it!</button>
</form>
What am I doing wrong, how come when I hit submit the function is not working?
Something like this, maybe? (I didn't check through all the code you linked to.)
<?php
function itg_fetch_img($img_url, ...) {
etc.
}
$img_url = $_POST['Stuff'];
if (!empty($img_url)) itg_fetch_img($img_url, ...);
?>
<form action="" method="post">
<input type="text" id="Stuff" name="Stuff" maxlength="30" value="<?=$img_url;?>" />
<input type="submit">Do it!</button>
</form>
Note that button is now an input with type submit, and javascript function is removed.
<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query