I would to submit 2 buttons in same php page, every button show deferent message on label,
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Test Submit 2 buttons</title>
<body>
<?php
$add_result = "";
if (isset($_POST['addnews']))
{
$add_result = "Hello, add new news";
}
if (isset($_POST['deletenews']))
{
$add_result = "Hello, delete all news";
}
?>
<br /> <br /> <br /> <br /> <br /> <br/>
<form accept-charset="utf-8" method="POST" dir="rtl" style="text-align:center" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Enter the news before click on any button::</label>
<br /> <br />
<input type="text" name="news_text" size="100" id="partNumber" required/>
<br /> <br /> <br /> <br />
<input type="submit" name="addnews" id="round" value="Add" />
<br /> <br/> <br />
<input type="submit" name="deletenews" id="round" value="Remove" />
<label for="message">
<?php echo $add_result; ?>
</label>
</form>
<style type="text/css">
input#round{
....
}
</style>
I used PHP_SELF in the form, I want the result show directly after click on any button without change the page
Thanks
You can't do anything with PHP without reloading the page, because each time it needs to be interprrted by server.
In case of 2 submit buttons both of them just submit the form, irrelevant what is the button name, and on the server-side you can't know, what button was pushed.
Either do some tricky js stuff, like changing some hidden input value or form action etc.
Or just keep it simple and use radio boxes
Related
Was trying to follow a tuitorial online on how to submit data using html to mysql database.
I created 2 php files named index.php and process.php. What i wanted to show was what I typed in inside index.php will show on process.php when I clicked on the button "add employee". But nothing showed up.
Here is what's inside index.php
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Add Employee</title>
</head>
<body>
<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add Employee">
</form>
</body>
</html>
Here is a Screenshot of the form.
click here for index.php image
Wile here is what's inside the process.php
<?php
print_r($_POST);
This is what shows up
process.php image
Sorry a beginner at this. Hope you guys can help. Thank you!
Maybe you are missing the "action"?
<form method="post" action="/process.php">
You should specify what you need.Somethings like this:
<?php
print_r($_POST['first_name']);
Or use foreach loop to get full array value.
You can go through with fill up action tag so when you submit the form it will go to proccess.php page.
<form method="post" action="process.php">
I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester
For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.
You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>
You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example
To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>
index.php
<html>
<head>
<script type="text/javascript">
function submitForms()
{
document.forms["form-1"].submit();
document.forms["form-2"].submit();
}
</script>
</head>
<body>
<form method="POST" action="form.php" id='form-1'>
<input type="text" name="txt1" />
</form>
<form method="POST" action="form.php" id='form-2'>
<input type="text" name="txt2" />
</form>
<input type="button" value="Click Me!" onclick="submitForms();" />
</body>
</html>
form.php
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Above is my code and when i submit both forms then both text-fields with their value it does not shoe me both text-field values.It only shoe me second text-field value.Please help me quickly.
I think because you try to get the params after sumbit two forms. You have sent the two forms at once and the second has stepped to the first, so the result is the return of the second form.
I think this will be better:
<html>
<head>
</head>
<body>
<form method="POST" action="form.php">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="Click Me!" />
</form>
</body>
</html>
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Sorry for my english
I am trying to build a simple HTML form that is spread over two pages.
First page asks for user's first and last name. Second page asks for his country & whether he would like to receive additional info.
The first page has a single button named "Next" for going to the second page. The second page has two buttons named "Next" to go the next page (non-existent at present) and "Back" to go back to the first page.
On the first page when I click the "Next" button, nothing happens. How can I go to the next page on clicking it ?
Here is my code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(!isset($_POST['next1']))
Display1();
else if(isset($_POST['next1']))
Display2();
else if(isset($_POST['back1']))
Display1();
?>
<?php
function Display1()
{
?>
<form id="form1" action="Multi.php" method="post">
<label>Enter First Name: </label>
<input type="text" name="text1" />
<br />
<br />
<label>Enter Last Name: </label>
<input type="text" name="text2" />
<br />
<br />
<input type="button" name="next1" value="Next" />
<?php
}
function Display2()
{
?>
<form id="form2" action="Multi.php" method="post">
Select Country
<select name="country[]" multiple="multiple">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="China">China</option>
</select>
<br />
<br />
Do you want to receive latest info ?
<input type="checkbox" name="checkbox1[]" />Yes
<br />
<br />
<input type="button" name="next2" value="Next" />
<input type="button" name="back1" value="Back" />
<?php
}
?>
</body>
</html>
The input type needs to be "submit"
<input type="submit" name="next1" value="Next" />
So for all the inputs that has "button" you need to change to "submit"
And as an addition to Abhik's answer.
<input type="button"> is only a clickable input. You (mostly) use it when you want to call a JS script. So if for some obscure reason you want to use this instead of the more simple solution provided by Abhik go ahead and add a onclick event handler.
<input type="button" onclick="document.getElementById("form1").submit();">
You could also use a <button> instead, but make sure you always specify the type. The advantage is that they offer a higher degree of customization. Basically you can put HTML inside a <button> tag.
So try also:
<button type="submit" value="Next">Next</button>
The other types of buttons are (source: w3schools.com):
button: The button is a clickable button
submit: The button is a submit button (submits form-data)
reset: The button is a reset button (resets the form-data to its initial values)
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script>
function c(){}
function test(){
if(document["f1"]["chkGuar"].checked){
document.getElementById("myrow").style.visibility="visible"
}
else{
document.getElementById("myrow").style.visibility="hidden"
}
}
</script>
</HEAD>
<BODY>
<FORM NAME="f1">
<input type="checkbox" name="chkGuar" value="ON" onclick = "c() ; test()">
<div id="myrow" style="visibility:hidden">
<input type = text name ='txtGRTNo' tabindex = 19 size="20"></div>
<div width="338" align="left" colspan="3" height="12"></div>
</FORM>
</BODY>
</HTML>
Here in this code, the text box opens in the same page but I want my text box to be displayed in another page (usually in the action page). I am working in a bus website, I want, when an user checks a checkbox (1 or more) to select a seat, I want that no of text boxes to be opened in the next page after the check boxes checked.
Here is the code you want. check it and let me know.
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<FORM NAME="f1" action="next_page.php" method="post">
<input type="checkbox" name="chkGuar[]" value="mike"> Mike<br />
<input type="checkbox" name="chkGuar[]" value="joy"> Joy<br />
<input type="checkbox" name="chkGuar[]" value="harry"> harry<br />
<input type="checkbox" name="chkGuar[]" value="watson"> watson<br />
<input type="checkbox" name="chkGuar[]" value="george"> george<br />
<input type="checkbox" name="chkGuar[]" value="peter"> Peter<br />
<input type="submit" name="chksbmt" value="Send" />
<!-- <div id="myrow" style="visibility:hidden">
<input type = text name ='txtGRTNo' tabindex = 19 size="20">
</div>
<div width="338" align="left" colspan="3" height="12"></div> !-->
</FORM>
</BODY>
</HTML>
next_page.php
<?php
if(isset($_POST['chksbmt'])){
$counts = count($_POST['chkGuar']);
echo "this is the next page. you checked $counts checkbox <br /><br />";
for($i=1;$i<=$counts;$i++){
echo "<input type='text' style='border:1px solid #000;' value='your text box here'
/><br/><br/>";
}
}
Hi this is code like your requirement try it
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
function c(){
alert("Hi");
this.f1.submit();
}
function test(){
if(document["f1"]["chkGuar"].checked){
document.getElementById("myrow").style.visibility="visible"
}
else{
document.getElementById("myrow").style.visibility="hidden"
}
}
</script>
</HEAD>
<BODY>
<FORM NAME="f1" action="next_page.php" method="post">
<input type="checkbox" name="chkGuar" value="ON" onclick = "c() ; test()">
<!-- <div id="myrow" style="visibility:hidden">
<input type = text name ='txtGRTNo' tabindex = 19 size="20">
</div>
<div width="338" align="left" colspan="3" height="12"></div> !-->
</FORM>
</BODY>
</HTML>
next_page.php
<?php
if(isset($_POST['chkGuar'])){
echo "this is the next page. <br />";
echo "<input type='text' style='border:1px solid #000;' value='your text box here' />";
}
You have to create a second page that get the request.
On the first one, make your for to post to the second path (action and method attributs that are missing in your code).
And then display in the textbox in the second page the content you want from the first one, based on the checkbox checked.
This is the way I see.