My html form automatically passing all field data in url - php

I have created a simple html form but I have encountered a weird problem which I have never seen before. When I click on submit then all of the data is directly passing to the URL bar. I don't know what is wrong with this can anyone help?
Form:
<form id="form-data">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>

the form uses get method therefore it displays the form values in the url. change the method to post to avoid this
method="post"
<!DOCTYPE html>
<html>
<head>
<title></title>
<body>
<form id="form-data" method="post">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>
</body>
</html>

Add method attribute to your form tag like this:
<form action="welcome.php" method="post">
https://www.w3schools.com/tags/att_form_method.asp

If you don't send the data somewhere, it gets passed as URL parameters to the current page:
From MDN Web Docs:
The action attribute defines where the data gets sent. Its value must
be a valid relative or absolute URL. If this attribute isn't provided,
the data will be sent to the URL of the page containing the form — the
current page.

Related

Use form button to submit GET value

I have the following code to submit GET values:
<form method="GET">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" />
</form>
<form method="GET">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" />
</form>
<form method="GET">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" />
</form>
Which is working fine. So I am not repeating myself with three forms, is there a way I could have a single form, and define what GET values are submitted by the buttons? They would still need to have the same information visible to the user e.g. the button that submits DESC needs to display as Price High to Low
use this code:
<form method="GET">
<input type="submit" name="price" value="ASC">Price Low to High</input>
<input type="submit" name="price" value="DESC" >Price High to Low</input>
<input type="submit" name="price" value="" >Default</input>
</form>
If having three buttons is not obligatory by design, then I suggest, why not just use a select to let the user decide what he wants (as most of the sites already do)
<form method="GET">
<select id="price" name="price">
<option value="">Default</option>
<option value="ASC">Price Low to High</option>
<option value="DESC">Price High to Low</option>
</select>
<input type="submit" value="Price Low to High" />
</form>
Demo
And if having three buttons is necessary then, you could do either one of (though not limited to) the following..
Use plain links (<a> anchor tags), yup you read that correct, since the form is just submitting using the $_GET method, eventually the data is gonna be inside the URLs, so why not just have it there in the first place using the href attributes of the "buttons" ?
Use the solution that #Aref Anafgeh suggested, and have three submit buttons each with different values and just let the html handle which value is sent to the server.
Use JavaScript and make ajax calls, which pretty much allows you to handle what is & isn't sent in the request.
Try this code.
<!DOCTYPE html>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
echo "Form Submited by ".$_GET['submitbutton']." Button";
}
?>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" name="submitbutton"/>
</form>
<script>
</script>
</body>
</html>
<form method="get">
<select name="sort">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
<option value="default">Default</option>
</select>
<button name="price">Sort price ascending</button>
</form>
snip
<?php if ($_GET['sort'] == "asc") {
// Do something
} elseif ($_GET['sort'] == "desc") {
// Do something else
}
// etc
?>
Hyperlinks to http://url/?price=XXX would also work fine since you're using GET

Form $_POST is empty (sometimes) but raw post data is fine

Heres my form:
<form method="POST" enctype="multipart/form-data" action="/my-account/update">
<select name="userType" class="form-control" >
<option value="0">0</option>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
</select>
<select name="userIsInSearch" class="form-control" >
<option value="1" selected="selected">1</option>
<option value="0">0</option>
</select>
<input type="text" class="form-control" id="fwn" name="signupfname">
<input type="text" class="form-control" id="sn" name="signupsname">
<input type="text" class="form-control" id="usrf" name="signupemail">
<input type="password" class="form-control" id="pwdf" name="signuppass">
<input type="password" class="form-control" id="pwdfc" name="signuppassconf">
<input type="submit" value="Update!" class="btn btn-green" />
</form>
I've noticed that 9/10 the form POSTs completely fine but sometimes it has a problem, I've noticed this on another form on the website too and I cant figure out what is causing it.
I have found a way to make the problem happen everytime, by simply typing "alert(" (without quotations) into any of the input fields causes the entire POST to be empty but php://input is fine.
The system is in codeigniter and I have tried dumping $_POST and php://input at the top of the index.php file and have the same results so I dont think it is codeigniter causing it.
Any help or ideas greatly appreciated!

How to identify a form field type in php

<form name="form" action="" method="get">
<input type="text" name="name" id="name" value="My name">
<textarea name="about_me" id="about_me"></textarea>
<input type="radio" name="gender" value="male" />
<input type="radio" name="gender" value="female" />
<select name="level">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Expert">Expert </option>
</select>
</form>
I have form like this,
My form fields are dynamically added.
After submitted the form, i need to identify the field type, needs to know as name is textbox value, about_me is textarea input, gender is radio option, level is dropdown etc..
is that any way to find out form field type in php.
First: take MyWay's approach. It is straight-forward and dead simple. However, if you want to build up a more complex structure, you could use the following code. It sets up hidden fields as an array and holds the name and type, separated by a ::
HTML Page:
<form name="form" action="" method="get">
<input type="hidden" name="fields[]" value="name:text">
<input type="hidden" name="fields[]" value="about_me:textarea">
<input type="hidden" name="fields[]" value="gender:radio">
<input type="hidden" name="fields[]" value="level:select">
<input type="text" name="name" id="name" value="My name">
<textarea name="about_me" id="about_me"></textarea>
<input type="radio" name="gender" value="male" />
<input type="radio" name="gender" value="female" />
<select name="level">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Expert">Expert </option>
</select>
</form>
Afterwards, in your PHP file:
$fields = $_POST["fields"];
foreach ($fields as $field) {
list($name, $type) = explode(':', $field);
$val = (!empty($_POST[$name]))?$_POST[$name]:"";
if ($type == "textarea") {
// do sth. useful with it
// the value is in $val (if there's one)
}
}

getting object not found error after clicking submit button, I want to display the value of hidden tag

<html>
<BODY>
<form action= "myscript.php" method="post">
first name: <input type="text" name="firstname" value="Mary"><br>
Last name: <input type="text" name="lastname" value= "clan"><br>
<input type="checkbox" name="mychoices[]" value="choice1" checked>
<input type="checkbox" name="mychoices[]" value="choice2">
<input type="checkbox" name="mychoices[]" value="choice3">
<select name="myselection">
<option value="selection1" selected>Option1</option>
<option value="selection2">Option2</option>
<option value="selection3">Option3</option>
</select>
<select name="myselections[]" size="3" multiple>
<option value="choice1" selected >Choice1</option>
<option value="choice2" selected>choice2</option>
<option value="choice3">choice3</option>
</select>
<textarea name="mytextarea" rows="10" cols="40">
Welcome to the web developement world.
</textarea>
<input type="password" name="mypassword">
<input type="hidden" name="myname" value = "myvalue">
<input type="reset" value="reset form">
<input type="image" name="myimage" src="desert.jpg" height="42" width="42" onclick= "document.write('<? php Aftersubmit() ?>');"/>
<input type="submit" name="submitbutton" value="Submit Form">
</form>
<?php
function Aftersubmit()
{
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
</BODY>
</HTML>
I want to display the value of hidden tag after clicking submit button. But getting "Object not found" error 404. Beginner in php, pls help. I also want to know how to call php functions from html.
You can't call a PHP function from an onClick event. That only works with Javascript functions. So one problem is in this line:
onclick= "document.write('<? php Aftersubmit() ?>');
You can remove that, and then pull your PHP out of the function. This will display your name if you click the submit button.
<?php
if(isset($_POST['submitbutton'])){
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
You may also have to change the action of your form:
<form action= "myscript.php" method="post">
If the file myscript.php doesn't exist, then you'll get a 404 Not Found error every time. You can make a form point to itself by removing the action attribute:
<form method="post">

php echo not working properly

Basically, I want made a simple form in html and i want to output the all of the user input once clicked submit using php. So far its just displaying the details I entered but it doesn't get user's data to echo them out. Here are my two files
output.php:
<?php
echo 'Name: '.$POST["name"].'<br /> Gender: '.$POST["gender"].'<br />Country: '.$POST["country"];
?>
testingformphp.html (form part):
<form id="form" name="form" method="post" action="./PHP/output.php"/>
Name<input name="name" type="text" id="name"/><br/>
Gender<label><input type="radio" name="gender" id="gender_0" value="male"/>Male</label><br/>
<label><input type="radio" name="gender" id="gender_1" value="female"/>Female</label><br/>
<select name="country" id="select">
<optgroup title="europe">
<option value="Russia">Russia</option>
<option value="Greece">Greece</option>
</optgroup>
</select>
<input type="submit" id="button" value="Submit"/>
<input type="reset" id="reset" value="Reset"/>
</form>
Can anyone help?
$POST does not exists, try $_POST.
<?php
echo 'Name: '.$_POST["name"].'<br /> Gender: '.$_POST["gender"].'<br />Country: '.$_POST["country"];
?>
http://www.tizag.com/phpT/postget.php To review the post and get method.

Categories