I am trying to create two submit button in my form
<form id="form-input-wrapper" action='test.php'>
//form items..
//form items..
<button class="btn btn-primary" type="submit" value="old">first button.</button>
<button class="btn btn-primary" type="submit" value="new">second button</button>
</form>
My question is how to distinquish which button the user clicks in my test.php page?
You need to add the name attribute to your buttons
<button class="btn btn-primary" type="submit" name="old" value="old">first button.</button>
<button class="btn btn-primary" type="submit" name="new" value="new">second button</button>
The php code to use would look like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['old'])) {
//old
} elseif (isset($_POST['new']){
//new
}
}
related question:
How can I tell which button was clicked in a PHP form submit?
Use button name tag and check them in php:
<button name="subject1" type="submit" value="HTML">HTML</button>
<button name="subject2" type="submit" value="CSS">CSS</button>
And in php:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ( isset($_POST['subject1']) ) {
// first button is clicked
} elseif ( isset($_POST['subject2']) ) {
//second button is clicked
}
}
?>
Related
I have a table where I use an UPDATE button with action form to update the data. But I also need to submit the table using another form action and submit button.
Here's my button:
<button class="btn btn-info" type="submit" name="update" value="update">Update Table</button>
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req">Submit Request</button>
This is currently my form action for the table:
<form method="post" action="">
And this is my PHP if else statement. The first submit_req is supposed to POST to another URL. But I don't know how to do it. I already tried header, but it won't work.
<?php
if(isset($_POST['update'])){
if(!empty($_SESSION['cart'])){
foreach($_POST['quantity'] as $key => $val){
if($val==0){
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
}elseif (isset($_POST['submit_req'])) {
//form action: insert_order.php
}
?>
You can use a JS function to redirect to another page for the submit button
Here page.php is where you want to send your form to. formID is the id of the form.
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req" onclick="submitForm('page.php')">Submit Request</button>
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('formID').action = action;
document.getElementById('formID').submit();
}
</script>
I need one help.I need to totally remove the submit button after submit the form and when it will be submitted the button will display to user.I am explaining my code below.
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px"> Name :</span>
<input type="text" name="u_name" id="name" class="form-control" placeholder="Add Name" onKeyPress="clearField('name');">
</div>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>
</form>
complain.php:
require_once("./include/dbconfig.php");
if(isset($_REQUEST['complainSubmit']))
{
// ......data is collecting here....
}
when data are submitted successfully the below part is executing.
<script type="text/javascript">
var phpVar = "<?php echo $_GET['success'];?>";
//console.log('php',phpVar=='');
if(phpVar == 1 && phpVar!=''){
alert('Submitted successfully.');
//var subButton=document.getElementById('addProfileData');
//subButton.disabled=false;
}
else if(phpVar == 0 && phpVar!=''){
alert('Unable to add.\\nTry again.');
}
else{
// nothing
}
</script>
<script>
function checkForm(){
var s=document.billdata;
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}
}
</script>
Here i need to button hide when the data is going to submit and it will again display after the submit.Please help me.
A javascript code such as:
document.getElementById("your_div").innerHTML = "";
will erase the content of your div.
So, byt tagging the entire form, then erasing its contents, you can "hide" it.
Why you dont add a hide CSS-Selector or remove it by checking via if-statement?
CSS-Version:
<input type="submit" class="btn btn-success <?php ($isSubmitted ? ' hideme' : '')?>" name="complainSubmit" id="addProfileData" value="Submit">
Except a new CSS-Selector: .hideme { display:none }
Via PHP/Template:
html...
<?php if(!$isSubmitted) : ?>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit">
<?php endif; ?>
html...
Dont forget: You dont need close input html-tag: <input/> just <input>
And what is wrong to hide the button via JS?
document.getElementById("addProfileData").style.display = "none";
UPDATE:
Prompt hiding after clicking:
<button onclick="javascript:this.style.display='none'">
Submit Button
</button>
in your example:
<input onclick="javascript:this.style.display='none'" type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>
I want add records into the base but not all. I have checkboxes and if some is checked then I add into the base if i click input button Save("Zapisz zmiany").
But input button save doesn't work. No reaction. POST array is empty. I don't know what i am doing wrong.
Input button:
<form action"" method="post" >
<input type="button" class="btn btn-primary" data-dismiss="modal" value="Zamknij/Close" />
<input type="button" class="btn btn-primary" name"zapros" id="zapros" value="Zapisz zmiany" />
</form>
and:
<?php
if (isset($_POST['zapros'])) {
$sqlc=mysqli_connect('127.0.0.1','root','');
if($sqlc) {
$sql_q="USE aplikacja";
mysqli_query($sqlc,$sql_q);
$data_zaproszenia = date("Y-m-d");
$sql_q=mysqli_query($sqlc, "SELECT * FROM goscie WHERE Email_goscia= '$mail'");
if(mysqli_num_rows($sql_q) != 0)
{
for ($i=1; $i < $lp ; $i++)
{
if (isset($_POST['zaznacz'.$i.'']))
{
$data_waznosci = $_POST['data_waznosci'.$i.''];
$sql_zaproszenie=mysqli_query($sqlc, "INSERT INTO zaproszenia (Email_goscia, ID_kwestionariusza, ID_wyniku, Data_zaproszenia, Data_waznosci, Wynik, Ocena, Status)
VALUES ('$mail', $id, NULL, '$data_zaproszenia', '$data_waznosci', NULL, NULL, 'Status')"); //dodawanie kolejnych odpowiedzi do bazy
echo "<script type='text/javascript'>alert('Zaproszenie wysłano!');</script>";
header('Refresh: 0;url=zaproszenia.php?id='.$id.'');
}
else
{
echo "<script type='text/javascript'>alert('Zaproszenie NIE wysłano!');</script>";
$i++;
}
}
}
mysqli_close($sqlc);
}
else
{
echo mysqli_connect_errno();
}
}?>
I haven't even examined your PHP, but you have some errors in your HTML:
<form action="" method="post" >
<input type="button" class="btn btn-primary" data-dismiss="modal" value="Zamknij/Close" />
<input type="submit" class="btn btn-primary" name="zapros" id="zapros" value="Zapisz zmiany" />
</form>
You were missing = signs in two places (action= and name=), and were using a button instead of a submit button.
I'm including a a php code with angular ui routing
the problem is if the millitary.php :
`
<div class="jumbotron">
<div class="page-header">
<h1>Millitary Loot Table</h1>
</div>
<?php
$MillitaryLoot = array(
'M4A4' => 47,
'AWP' => 2,
'Karambit' => 1,
'Famas' => 50
);
$newMillitaryLoot = array();
foreach ($MillitaryLoot as $item=>$value)
{
$newMillitaryLoot = array_merge($newMillitaryLoot, array_fill(0, $value, $item));
}
$myLoot = $newMillitaryLoot[array_rand($newMillitaryLoot)];
if (isset($_POST['submit']))
{
echo "\n" . "<h2 style='display:inline;'>Item: </h2>" . '<p class="text-center text-danger">' . $myLoot . '</p>' . "</br>";
}
?>
<form name="Gamble" action="" method="post" target="_self">
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
</br>
</div>
`
the include app.js
$stateProvider
.state('millitary', {
url: '/millitary',
templateUrl: 'inc/millitary.php'
})
is included into the index.php the submit button basicly not executes the code above the form
so basically i want the php code to execute in the index.php to keep the ui instead of jumping into the executing php file and losing the index.php linked styles and navigation
edit : if i run the code normaly it works but it doesn't if it gets included
edit2 : everything outside the if statement works basically if i add a echo "test"; it outputs but inside the if it does not so the problem is the form and the connection to the if statement
kind regards, daniel
Change your HTML to the following:
<form id="form" name="Gamble" method="post">
<button id="form" name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
You can submit your form with a button with HTML5 but it must share the same ID of the form. Also keep in mind this will only be supported with browsers that support HTML5 so if not they won't be able to submit the form.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
The other alternative is to use...
<input name="submit" type="submit" class="btn btn-primary" value="Gamble">
as #gbestard suggested.
Change
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
to
<input name="submit" type="submit" class="btn btn-primary" value="Gamble" />
In order to submit you need a type submit input or use some client side script as javascript
i have designed this form for login modal:
<tr>
<td></td>
<td><?php echo form_submit('submit', ' Log In', 'class="btn btn-primary btn-lg btn-block"'); ?>
<?php echo form_submit('forget', ' Forget Password ', 'class="btn btn-warning btn-lg btn-block"'); ?>
</td>
</tr>
both of these buttons are doing same works how can I isolate which one has pressed?
thanks for everyon
You can obtain this by using this HTML:
<tr>
<td></td>
<td>
<button type="submit" name="action" value="submit" class="btn btn-primary btn-lg btn-block"> Log In </button>
<button type="submit" name="action" value="forget" class="btn btn-warning btn-lg btn-block"> Forget Password </button>
</td>
</tr>
Then use the POST data like this:
$action = $this->input->post('action');
if($action == 'submit') {
// execute code to log in
}
if($action == 'forget') {
// execute code to forget the password
}
if (!empty($_POST['submit'])) {
// submit button pressed
}
if (!empty($_POST['forget'])) {
// forgot password button pressed
}
Have you tried this:
if($this->input->post('submit'))
{
// what to do in case of submit
}
if($this->input->post('forget'))
{
// what to do in case of forget
}
In the view use a multipart form instead of a normal form.Like this
<?php echo form_open_multipart('Controller/function')?>
Then simply use your submit buttons
Then again in the controller use post with submit fields names
if($_POST['SUBMIT1'])
{
//code
}
if($_POST['SUBMIT2'])
{
//code
}