can i put two pages into action=" " in php [duplicate] - php

I'm setting up a form wherein I need two "actions" (two buttons):
1 - "Submit this form for approval"
2 - "Save this application for later"
How do I create an HTML form that supports multiple "actions"?
EG:
<form class="form-horizontal" action="submit_for_approval.php">
<form class="form-horizontal" action="save_for_later.php">
I need to combine these two options-for-submitting into one form.
I did some basic research but couldn't find a definitive answer as to whether or not this is possible, and/or any good resources to links for a workaround.

As #AliK mentioned, this can be done easily by looking at the value of the submit buttons.
When you submit a form, unset variables will evaluate false. If you set both submit buttons to be part of the same form, you can just check and see which button has been set.
HTML:
<form action="handle_user.php" method="POST" />
<input type="submit" value="Save" name="save" />
<input type="submit" value="Submit for Approval" name="approve" />
</form>
PHP
if($_POST["save"]) {
//User hit the save button, handle accordingly
}
//You can do an else, but I prefer a separate statement
if($_POST["approve"]) {
//User hit the Submit for Approval button, handle accordingly
}
EDIT
If you'd rather not change your PHP setup, try this: http://pastebin.com/j0GUF7MV
This is the JavaScript method #AliK was reffering to.
Related:
2x submit buttons to action different URL
Submit form to another page (which is different from the page used in ACTION)

the best way (for me) to make it it's the next infrastructure:
<form method="POST">
<input type="submit" formaction="default_url_when_press_enter" style="visibility: hidden; display: none;">
<!-- all your inputs -->
<input><input><input>
<!-- all your inputs -->
<button formaction="action1">Action1</button>
<button formaction="action2">Action2</button>
<input type="submit" value="Default Action">
</form>
with this structure you will send with enter a direction and the infinite possibilities for the rest of buttons.

This should work without changing the backend code:
<form class="form-horizontal" action="submit_for_approval.php">
<button>Submit for Approval</button>
<button formaction="save_for_later.php">Save for Later</button>
</form>
The accepted answer didn't work for me because I'm using Golang and apparently the default Go form parsing returns missing variables the same as empty ones (as empty strings). So you need to split it into separate endpoints.

this really worked form for I am making a table using thymeleaf and inside the table there is two buttons in one form...thanks man even this thread is old it still helps me alot!
<th:block th:each="infos : ${infos}">
<tr>
<form method="POST">
<td><input class="admin" type="text" name="firstName" id="firstName" th:value="${infos.firstName}"/></td>
<td><input class="admin" type="text" name="lastName" id="lastName" th:value="${infos.lastName}"/></td>
<td><input class="admin" type="email" name="email" id="email" th:value="${infos.email}"/></td>
<td><input class="admin" type="text" name="passWord" id="passWord" th:value="${infos.passWord}"/></td>
<td><input class="admin" type="date" name="birthDate" id="birthDate" th:value="${infos.birthDate}"/></td>
<td>
<select class="admin" name="gender" id="gender">
<option><label th:text="${infos.gender}"></label></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
<td><select class="admin" name="status" id="status">
<option><label th:text="${infos.status}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td><select class="admin" name="ustatus" id="ustatus">
<option><label th:text="${infos.ustatus}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td><select class="admin" name="type" id="type">
<option><label th:text="${infos.type}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select></td>
<td><input class="register" id="mobileNumber" type="text" th:value="${infos.mobileNumber}" name="mobileNumber" onkeypress="return isNumberKey(event)" maxlength="11"/></td>
<td><input class="table" type="submit" id="submit" name="submit" value="Upd" Style="color: white; background-color:navy; border-color: black;" th:formaction="#{/updates}"/></td>
<td><input class="table" type="submit" id="submit" name="submit" value="Del" Style="color: white; background-color:navy; border-color: black;" th:formaction="#{/delete}"/></td>
</form>
</tr>
</th:block>

In front-end:
<form action="act1.php" method="post">
<!-- Your HTML Code -->
<button type="submit" name="act" value="action1">Submit</button>
<button type="submit" name="act" value="action2">Save for Later</button>
</form>
In Backend: (act1.php)
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$check = $_POST['act'];
if($check == "action1") {
/* Write the code of "submit_for_approval.php" Here or add the following line */
header("Location: submit_for_approval.php");
}
if($check == "action2") {
/* Write the code of "save_for_later.php" Here or add the following line */
header("Location: save_for_later.php");
}
}
?>

Related

Cannot echo variables after form submittiom

So I am submitting a form to the same page. I have used the following code to check whether the form was submitted, and to avoid 'undefined variables' errors during the first page reload.
if (isset($_POST['submit'])){
// Get search variables
$pName = $_POST['pname'];
$pLocation = $_POST['plocation'];
$pPrice = $_POST['pprice'];
if (isset($_POST['ptype'])){
$pType = $_POST['ptype'];
}
echo "pType";
}
However, I cannot echo the php variables onto the page. I am guessing this is because the form is refreshed when it is sent by post, so the variables are lost.
How can I fix this problem?
This is the html form
<form method="post" action="../html/searchpage.php">
<div id="searchborder">
<input type="text" id="pname" name="pname" placeholder=" Property name">
<input type="text" id="plocation" name="plocation" placeholder=" Property location">
<input type="number" id="pprice" name="pprice" placeholder=" Property price">
<div id="ptypeholder">
<div id="ptypebox">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
</div>
</div>
<button type="submit"><img src="../images/search.png"></button>
</div>
</form>
I am trying to display 'property posts' into html cards from a database using php in this page. The form is the search bar for the property posts.
Thanks!
I don't think $_POST['submit'] is being set because the button needs a name ie
<input type="submit" name="submit" value="submit">
also you have a few other syntax errors, structure, may be this will help :-
<form method="post" action="searchpage.php">
<input type="text" id="pname" name="pname" placeholder="Property name">
<input type="text" id="plocation" name="plocation" placeholder="Property location">
<input type="number" id="pprice" name="pprice" placeholder="Property price">
<select name="ptype">
<option value="" disabled selected>Post type</option>
<option value="buy">Buy</option>
<option value="rent">Rent</option>
</select>
<input type="submit" name="submit" value="submit">
and you don't HAVE to assign the post values to $vars you can use them directly -
<?php
if (isset($_POST['submit'])){
// Get variables
//$pName = $_POST['pname'];
//$pLocation = $_POST['plocation'];
//$pPrice = $_POST['pprice'];
echo $_POST['ptype'].'<br>';
echo $_POST['pname'].'<br>';
echo $_POST['plocation'].'<br>';
echo $_POST['pprice'].'<br>';
}
?>
Needs more work to make it check all posts are set or validation but works as a basic starting point
Also in original you posted echo "pType"; will only echo the text pType not the value

Reading Data from two PHP pages into the third Page

I'm not able to combine the data from two pages. I'm only able to show data from one page. I tried searching for an explanation of my problem on google but I could not find it.
I get an "Undefined index" error as you can see [![in this screenshot][1]][1].
Could not put just code over here because it keeps telling me that I need to use spacing with ctrl + K and im newbie in all this things so please forgive me..
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>
<form action = "page2.php" method = "POST">
<select list="Country" placeholder="Country" name="country" required class="form-control" style="max-width:250px; margin-top:50px;" id="Country">
<option >Bosnia & Herzegovina</option>
<option >Croatia</option>
<option >Serbia</option>
<option >England</option>
<option >Germany</option>
<option >Austria</option>
<option >Belgium</option>
<option >Switzerland</option>
<option >Italy</option>
<option >Romania</option>
<option >France</option>
<option >Montenegro</option>
<option >Slovenia</option>
</select>
<input type="email" name="email" id="email" placeholder="Your email address" required class="form-control" style="max-width:250px;" />
<input type="password" name="password" id="creapass" placeholder="Create your password" required class="form-control" style="max-width:250px;" />
<input type="password" name="password2" id="password" placeholder="Confirm your password" required class="form-control" style="max-width:250px;" />
<input type="button" value="Next" class="btn btn-primary" style="width:150px;">
</form>
</body>
</html>
Second Page
<!DOCTYPE html>
<html>
<head>
<title>test2</title>
</head>
<body>
<form action = "finalpage.php" method = "POST">
<!-- Values from First Step -->
<input type="hidden" name="country" value="<?php $_POST['country'] ?>">
<input type="hidden" name="email" value="<?php $_POST['email'] ?>">
<input type="hidden" name="password" value="<?php $_POST['password'] ?>">
<input type="hidden" name="password" value="<?php $_POST['password2'] ?>">
<!-- End of Values from First Step -->
<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";
?>
<input type="text" name="first" class="form-control" placeholder="First name" required style="max-width:250px;" >
<input type="text" name="last" class="form-control" placeholder="Last name" required style="max-width:250px;" />
<input type="date" name="date" class="dateb" id="dateOfBirth" required />
<select list="Country" placeholder="Country" name="country2" required class="form-control" style="max-width:250px; " id="Country">
<option >Bosnian</option>
<option >Croat</option>
<option >Serb</option>
<option >English</option>
<option >German</option>
<option >Austrian</option>
<option >Belgian</option>
<option >Swiss</option>
<option >Italian</option>
<option >Romanian</option>
<option >French</option>
<option >Montenegrin</option>
<option >Slovenian</option>
</select>
<input type="text" class="form-control" name="street1" placeholder="Street address 1" required style="max-width:250px;" />
<input type="text" class="form-control" name="street2" placeholder="Street address 2 (Optional)" style="max-width:250px;" />
<input type="text" class="form-control" name="city" placeholder="City" required style="max-width:250px;" />
<input type="text" class="form-control" name="region" placeholder="Province/Region" required style="max-width:250px;" />
<input type="text" class="form-control" name="postal" placeholder="Postal code" required style="max-width:250px;" />
<input type="tel" class="form-control" name="phone" placeholder="Phone number" required style="max-width:250px;" />
<input type="checkbox" name="terms" > I have read and agree to the Terms and WBC's User Agreement.
<input type="Submit" name="insert" value="Continue" class="btn btn-primary btn-lg">
</form>
</body>
</html>
Final Step.
<html>
<head>
<title>get data from another page</title>
</head>
<body>
<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";
echo "First name:" .$_POST["first"]."</br>";
echo "Last name:".$_POST["last"]."</br>";
echo "Date:".$_POST["date"]."</br>";
echo "Country2:".$_POST["country2"]."</br>";
echo "Street1:" .$_POST["street1"]."</br>";
echo "Street2:".$_POST["street2"]."</br>";
echo "City:".$_POST["city"]."</br>";
echo "Region:".$_POST["region"]."</br>";
echo "Postal code:" .$_POST["postal"]."</br>";
echo "Phone number:".$_POST["phone"]."</br>";
?>
</body>
</html>
I am not copying the entire code of yours as it is not required. Below is the logic you have to follow.
In the first step, i.e. your first form (I prefer using PHP format as it allows you to use PHP functions, if required), put your second PHP in the form's action.
<form action = "step_2.php" method = "POST">
Now in your step_2.php, you have to add the below in your form.
<!-- Values from First Step -->
<input type="country" name="country" value="<?php echo $_POST['country'] ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email'] ?>">
<input type="hidden" name="password" value="<?php echo $_POST['password'] ?>">
<!-- End of Values from First Step -->
Above code will add the values posted from the first step into your second step form. Now you can simply fetch these values in the final step. To do this, add your final step PHP in your Action of this form.
<form action = "final_step.php" method = "POST">
In your final_step.php, you can normally get the values by $_POST.
You've got a number of problems. First, your option fields don't have values.
What you have: <option>Bosnia</option>
What you need: <option value="bosnia" >Bosnia</option>
You should also have a default as your first option, otherwise your program is going to assume that everyone who doesn't pick something has the first option, and you are going to get a million submissions that are not accurate. Eg:
<option value="false" selected="selected">Please select an option from below</option>
Then your backend should be checking like this:
<?php
if (!isset($_POST['country'] || $_POST['country'] === 'false')
{
//They did not pick a country, and you need to bounce them back to the form with an error message.
header('Location: /path/to/your/form');
exit;
}
//country is defined, you can continue,
//but you should pre-validate the other required field here also.
//do not assume things are right, assumptions = bugs.
...
The anchor tag you have surrounding your submit button may be redirecting instead of submitting the form, in which case your $_POST will be completely empty. The below tag you have:
<input type="button" value="Next" class="btn btn-primary" style="width:150px;">
Needs to look more like this:
<input type="submit" value="Next" class="btn btn-primary" style="width:150px;">
Or alternately:
<button type="submit" class="btn btn-primary" style="width:150px;">Next</button>
If you are stumped, put this at the beginning of what you have on your processing code:
echo '<pre>' . print_r($_POST, 1); exit;
This will show you what was submitted, and let you work through from there as needed.
In general, your conditionals should check if a key exists before validating it if there is any ambiguity as to whether or not it is set:
//BAD
if ($_POST['country'] == 'someval') { ... }
//GOOD
if (array_key_exists('country', $_POST) && $_POST['country'] == 'someval') { ... }
You can also check with isset($_POST['country']); HOWEVER, if null values are valid for whatever you are checking, then this will return false, even if the key is there. So in any case where null is valid, you need to use array_key_exists() instead.
Instead of passing variables directly from one page to another, it's a lot easier if you use $_SESSION, so if they derp up and hit back or walk away for a while, the values are not entirely lost. Users hate having to redundantly redo things, and will probably entirely lose interest in your site if they have to do that. Instead, you can set the values if they exist into session memory like this:
<?php
session_start(); //you have to do this before printing ANY other content.
$_SESSION['country'] = (isset($_POST['country']) ? $_POST['country'] : null);
And then you can retrieve this at any time from any page by doing:
<?php
session_start();
echo $_SESSION['country'];
and if you want to clear them, you just do:
<?php
session_start(); //can't destroy a non-existent session, you have to do this first
session_destroy(); //clears the session
session_start(); //starts a new one
Stashing ongoing form values in the session temporarily lets you access them for as long as the session cookie lasts, so they are not lost if the user quits your form, goes to another site, then comes back to finish. Your form will have to put these back in manually though, so an input might look something like this
<input type="password" name="password" value="<?= isset($_SESSION) && isset($_SESSION['password'] ? $_SESSION['password'] : null; ?>" />
I tried to explain the relevant logic you are not applying rather than just the specific use case, because without actually grasping what the underlying lack of logic is, you would otherwise probably just hit the same wall again. Hopefully this saves you from a number of other bugs that you would otherwise likely hit immediately after this specific thing gets fixed for you.

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

2 forms on same page php

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values

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">

Categories