How to stop submit being shown in the address bar? - php

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

Related

switch function to change page based on user input not working

I am trying to change page based on user input in form. The user enters their tag and it changes to that users page with details. Currently, it just keeps attempting to change page then eventually times out. Here is my code:
SWITCH STATEMENT
if(isset($_POST['submit'])) {
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
...//
default:
header("Location: index.php");
}
}
FORM
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
So if user enters player 1 in form then submits, it should change to commander.php page, but it isn't.
Could anyone point me in correct direction thanks.
First of all, change first line to
if(isset($_GET['submit'])){
2nd give a 'name' attribute to submit button
<input type="submit" class="submit" value="SUBMIT" name="submit"/>
I just looked at your code and realized your form was sending data using the HTTP GET method but your PHP script was checking for HTTP POST method in the if(isset($_POST['submit'])). So I modified the PHP as shown below.
if(isset($_GET['clan_tag'])){
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
default:
header("Location: index.php");
}
}
?>
I also modified the HTML as shown below.
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
Try in this way:
In the FORM:
attribute method write "post" in action= write "path where is your php file"
i.e.
<form action="path/yourfile.php" method="post">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
in PHP file
if(isset($_POST['clan_tag'])) {
$name = $_POST['clan_tag'];
}
else{
$name="";//your code
}
....

php multipage input value session variable print

please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">
Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);

PHP - cannot get value when input exists

I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?
You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>

Post Form DATA to another page after PHP Validation

I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Categories