PHP/HTML form-action attribute - php

I have a small problem. I have one form, for search. After submitting this form is can't stay on same page.
My url:
www.localhost/index.php?vehicletype=car
On this url, the car search is visible.
After the submit I get this:
localhost/index.php?body_type=any&fuel_type.....
But I want this,
www.localhost/index.php?vehicletype=car?body_type=any&fuel_type....
I tried $_SERVER["REQUEST_URI"] but nothing. Thanks for the help, and sorry for my English grammar!
EDIT: The code:
<? if($_GET['vehicletype']=='car'){ ?>
<div class="search">
<form method="get" action="">
....
<button class="btn btn-search" type="submit">Search <hr> (658 found)</button>
</form>
</div>

First thing following url is wrong
"www.localhost/index.php?vehicletype=car?body_type=any&fuel_type...."
after "?" it will set all argument in get method you can access that variable using $_GET['variablename']
if you want to be on same page you can action like following way
<form action="#" method="GET">
When someone click on submit button same page will be called with form params.
You can get all form param by using $_GET method.
Method 1.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
else
{
?>
Show HTML Form here
<?php } ?>
Method 2.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
<form action="#" method="GET">
<input type="text" name="vehicletype" value="<?php if (isset($_GET['vehicletype'])) { echo $_GET['vehicaletype']; } ?>">
</form>
Let me know if you have further query.

Related

Confused about creating a "login" form

So what I want to do it kind of like a login form, but rather than it being individual users, it's more of a password locked page.
Here's sort of what I have for php
<?php
$user = $_POST['user'];
if($user == "placeholder")
{
include("randomfile.html");
}
else
{
if(isset($_POST))
{?>
<form id="login" method="POST">
User <input type="text" name="user" id="userID"></input><br/>
<input type="submit" name="submit" value="Go"></input>
</form>
<?}
}
?>
and it's basically doing what I want it to do, but if you were to go back (like use the go back button in the browser) it doesn't get rid of that submitted text (in this case, it would be "placeholder").
Any suggestions of any other way to do this, maybe easier or more basic because I just started with php, and is it possible so that if you enter "placeholder" and submit it, then go back, it doesn't have the User field already filled out with what you previously submitted?
<form id="login" method="POST" autocomplete="off">
That work for all the form, I think is the easiest. Ref: form:autocomplete

How to do redirecting to another page using form in PHP?

<form method="post" action="register_enquiry.php">
This code is not redirecting to "register_enquiry" page. Please let me know what's wrong in this. It's giving a blank page.
Is your register_enquiry page empty? Did you add a submit button?
Try this for Your initial page.
<form method="POST" action="register_enquiry.php">
<input type="text" name="nameWanted">
<input type="submit" value="Submit">
</form>
the register_enquiry.php page.
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
// do want ever you want
}
?>
<p>Welcome, My register_enquiry.php page</p>
This has worked for me!
Use formaction="register_enquiry.php" on your submit button.May be that helps

embed html form to php code

I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}

Creating a iframe search page for my site

It is very difficult for me to put in words my query. But I will try.
I have a site xyz.com which has search facility for listed products. The search page url is generated like this :www.wyz.com/search/search_term
I want to create a iframe page in a third party site with a search facility which can directly communicated with my site xyz.com.
I have tried to create a search box with a submit button. I want to append the search query in as a variable to my form action url string.
So the search string should look like this :www.wyz.com/search/my_string_variable
The code I have written is:
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
}
?>
<?php
$result=$url.$r1
?>
<html><body>
<form action="<?php echo $result; ?>" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
==================================================================
But output what I get, is only "http://www.xyz.com/search/". It removes my variable from the url. I am not able to find what is the reason? I have also tried to print result via to check the actual output and it shows that it has added the value at the end of url. But when I want to achieve the same thing via form action it does not work. please help?
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
$result=$url.$r1;
header("location:$result");
}
?>
<html><body>
<form action="" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
Please try the above code. I have made some modifications. The main reason your code is not working is whenever you press the submit button it is going to the the url "http://www.xyz.com/search/" directly .The if condition is never executed. In the above mentioned code it will work properly
action="" - you are submitting to the wrong url. Here is alternate version -
<?php $url='http://www.xyz.com/search/';
if (isset($_POST['submit'])) {
$r1=$_POST['num1']; header("Location: ".$r1); // 302 redirection
}
?>
<html><body> <form target="_SELF" method="post"> Num1:<input name="num1" type="text" /><br /> <input type="submit" name="submit" /> </form> </body></html>

Target 'Search' in Whole Page

I want my search bar to complete a search, however, I want the target to be 'wholepage' and not to totally move to another page?
<div id="search_anything">
<form action="/search.php" method="get" id='searchForm'>
<div class="searchFormDiv">
<input type="text" name="search" value="Search Mail..." id="search" onfocus="if( $(this).attr('value').indexOf('...') >= 0) $(this).attr('value',''); $(this).select();" />
</div>
</form>
</div>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="get" id='searchForm'>
Don't forget that most of the time you need to process the form on the same page too (or include a separate form processor script on that page).
For example if you want to process the form with another PHP file, you can paste this anywhere above the form:
<?php if(isset($_GET['search'])) include 'formprocessor.php'; ?>
When I enter that code I get the following link: - liveternet.com/?search=(My search), however, I want: liveternet.com/search.php?search=(My Search) << including the search being complete in the same webpage :) (and not redirected to another)
Use jQuery:
$('form').submit(function() {
$.post(
'do_query.php', // File with query
{ num: '1', str: 'string' }, // Vars passed as post
function(responseText){ // Callback
$('#your_div').html(responseText); // Load content results from do_query page.
},
"html" // Set type as html
);
preventDefault(); // Prevent form from submiting
});
This should work. Please give feedback.

Categories