Show a button via PHP - php

I want to show the second button when i click on the first button , I've coded this but for an unknown reason it didn't work . hope anyone help me:
<input class="btn btn-success" type="submit" name="bb1" value="Show Button" "></input>
<a href="#">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if(isset($_POST['bb1'])){
echo "<input class=\"btn btn-success\" type=\"file\" name=\"bb1\" value=\"Show ME\" \"></input>
";?>

The problem has been solved, i just need to add the input inside the form.

Related

How to prompt for button press confirmation?

I'm loading data in to a html table using following function. It also creates a delete button in front of each row. I want to prompt user to either confirm or cancel. But the code does not work and it does not prompt for confirmation. Can someone show me how to do it properly?.
I use (onclick="return confirm('Are you sure?')") to prompt.
//This function will list categories
function listCategories($sqlString)
{
$result = mysqli_query($this->connectToDb(), $sqlString);
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['cat_id']."</td>";
echo "<td>".$row['it_category']."</td>";
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm('Are you sure?')">
</form>
</td>';
echo "</tr>";
}
}
}
You need to escape the quotes around Are you sure. They're matching the quotes around the string argument to echo.
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm(\'Are you sure?\')">
</form>
</td>';

PHP - Multiple form buttons with different actions

I have a simple form in HTML that contains two buttons. Button 1 which action in the form tag submits it to another php page e.g. button1-action.php which submits data to a third party API and Button 2 which I want to submit to the same page if it is clicked without going to button1-action.php.
In its simplest method the form is as follows:
<?php
echo '<form name="form123" id="form123" action="button1-action.php" method="POST">';
echo '<input type="text" name="first_name" id="first_name></input>';
echo '<button name="button1" id="button1" value="button1">Button 1</button>';
echo '<button name="button2" id="button2" value="button2">Button 2</button>';
echo '</form>';
?>
This is what I tried so far
$action = null;
if (isset($_POST['button1'])) {
$action = 'button1-action.php';
} elseif (isset($_POST['button2'])) {
$action = $_SERVER["PHP_SELF"];
}
echo '<form name="form123" id="form123" action="' . $action . '" method="POST">';
echo '<input type="text" name="first_name" id="first_name></input>';
echo '<button name=" button1" id="button1" value="button1">Button 1</button>';
echo '<button name="button2" id="button2" value="button2">Button 2</button>';
echo '</form>';
However, it doesn't seem to be working. I tried to look for solutions but I haven't been successful.
I'm interested in any solution, but I would prefer solving it using PHP and not JavaScript.
The Issue might be that you forgot to close the <form> tag with </form> and you should use the <input> for buttons aswell with type="submit" .
If this still doesn't resolve your issue then maybe you should try this :
On the same page.
<?PHP
//// place this on top
if($_POST["button1"]) {
// add code to send data to Third Party API
}
if($_POST["button2"]) {
// will show data here
} ?>
////////
<?php
echo '<form name="form123" id="form123" action="/">';
echo '<input type="text" name="first_name" id="first_name></input>';
echo '<input type="submit" name="button1" id="button1" value="button1" >';
echo '<input type="submit" name="button2" id="button2" value="button2" >';
echo '</form>';
?>
I hope this answers your question 😊
This is the solution for you in html
<form name="form123" id="form123" method = "post">
<input type="text" name="first_name" id="first_name"></input>
<button name=" button1" id="button1" value="button1" formaction="button1-action.php" >Button 1</button>
<button name="button2" id="button2" value="button2" >Button 2</button>
</form>
button 1 will submit the form to button1-action.php and button 2 will submit the form to same page.
Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction
You appear to be using the value submitted from the form to set the action of the form. This makes no sense - by the time you read the submitted values, the action has already happened. So your code would just set the action for next time the form is submitted. Not useful.
At the heart of this there seems to be a conceptual / design issue. A more sensible approach (but not the only one) would be to simply post the form to the same place every time, and then use if statements to decide what to do next.
e.g.
if (isset($_POST['button1'])) {
require_once "button1-action.php";
} elseif (isset($_POST['button2'])) {
//do whatever it is you want to do in ths script
}
else {
?>
<form name="form123" id="form123" method="POST">';
<input type="text" name="first_name" id="first_name></input>
<button name=" button1" id="button1" value="button1">Button 1</button>
<button name="button2" id="button2" value="button2">Button 2</button>
</form>
<?php
}
To improve a bit more on that, instead of using a bare require to include the code from another script, we could encapsulate the code from button1-action.php into a function which we can call, instead of a script with global scope. This makes the code more re-usable, maintainable, testable, less likely to cause scope conflicts, etc.
e.g.
if (isset($_POST['button1'])) {
callTheApi($_POST["first_name"]);
} elseif (isset($_POST['button2'])) {
doSomethingElse($_POST["first_name"]);
}
else {
?>
<form name="form123" id="form123" method="POST">';
<input type="text" name="first_name" id="first_name></input>
<button name=" button1" id="button1" value="button1">Button 1</button>
<button name="button2" id="button2" value="button2">Button 2</button>
</form>
<?php
}
(Even better if you then encapsulate that function in a class containing closely related functionality, but let's just get as far as a funtion for now.)
Alternatively, Virender Kumar's answer would also be reasonable - simply setting the form action of each button directly.
First of all your form is not structured properly.
index.php
<form name="form123" id="form123" action="button1-action.php">
<input type="text" name="first_name" id="first_name"></input>
<button name=" button1" id="button1" value="button1">Button 1</button>
<button name="button2" id="button2" value="button2">Button 2</button>
</form>
button1-action.php
if (isset($_GET['button1'])) {
echo 'button1 submitted'; // Send data to the third party API
} else if (isset($_GET['button2'])) {
echo 'button1 submitted'; // Submit on the same page
}
Edit: ignore my solution; Virender Kumar’s solution here is correct, elegant and doesn’t need JS.
Original answer:
The issue is not with your buttons, but with the fact that a form can only post to a single endpoint (the action attribute). You will have to handle what happens with the form data from there. If you truly want your form to be posted to a different endpoint in the client based on what button the user clicks, you can’t do it without JavaScript.
If you can live with JS, this could work:
<body>
<!-- your form here -->
<script>
const form = document.forms[0]; // assuming your form is the first form on the page, or the only one
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', event => {
if (event.target.name === 'button1') {
form.action = 'button1-action.php';
} else if (event.target.name === 'button2') {
form.action = 'other-destination';
}
});
});
</script>
</body>

cannot get $_POST variable value when HTML form is isset

i want to get the value of a text input 'name="quantity"' when my html form is isset using $_POST , the problem is everytime i submit the form i cannot get it's value !
HTML :
<form method="POST" name="updateform">
<!-- the input text that i want to get it's value when the form is isset -->
<input type="text" name="quantity" value="<?php echo $row['quantite'] ?>" size="1" class="form-control" />
<!-- the input text that i want to get it's value when the form is isset -->
<a type="submit" name="updateu" data-toggle="tooltip" title="Update" class="btn btn-primary" href='cart.php?id=<?php echo $getid ?>&upt=<?php echo $row['idproduit']; ?>' ><i class="fa fa-clone"></i></a>
</form>
PHP :
//update commande
if (isset($_POST['updateform'])) {
$mdf = $_POST['quantity'];
echo $mdf;
}else{
echo "form not isset()";
}
//update commande
it's showing "form not isset"
any solutions please , and thanks
You cannot use an anchor tag as a portion of the form, as you've done here:
<a type="submit" name="updateu" data-toggle="tooltip" title="Update" class="btn btn-primary" href='cart.php?id=<?php echo $getid ?>&upt=<?php echo $row['idproduit']; ?>' ><i class="fa fa-clone"></i></a>
The link never becomes part of the post array. You will need a submit input named updateu, for example:
<input type="submit" name="updateu" ...

HTML form POST method not working (despite URL params showing)

Got a form setup to submit some stuff.
There are three submit buttons, all with the same name (choice) and different IDs (1, 2 & 3).
Using POST method to submit the form to form.php
Form.php loads and I can see the form params in the URL.
However there is no POST data coming in.
Index.php:
<form action='form.php' action='POST'>
<input type='hidden' name='index' value='".$cell_count."'>
<div class='btn-group btn-group-m'>
<button name='choice' value='1' type='submit' class='btn btn-default btn-danger'>
...
</button>
<button name='choice' value='2' type='submit' class='btn btn-default btn-warning'>
...
</button>
<button name='choice' value='3' type='submit' class='btn btn-default btn-success'>
...
</button>
</div>
<button name='choice' type='submit' value='4' class='btn btn-default btn-sm'>
...
</button>
Form.php :
<?php
var_dump($_POST);
if($_POST['choice'] == 3) {
echo "Chose 3";
}
else if($_POST['choice'] == 2) {
echo "Chose 2";
}
else if($_POST['choice'] == 1) {
echo "Chose 1";
}
echo "index: " . $_POST['index'];
?>
Result :
http://i.stack.imgur.com/JrB7j.png
Thanks for any help you can offer!
You are mixing the attributes:
<form action='form.php' action='POST'>
Should be:
<form action='form.php' method='POST'>
Now you don't have a method attribute, so the form is sent by GET (the default).
You're form should be using method="POST". The fact that you are seeing the parameters means that your form is submitting as GET.
The form code should be:
<form action='form.php' method='POST'>
The action isn't supposed to be post, the METHOD is post.
<form action='form.php' method='post'>

How do I give a value to an href as its done to a submit?

I named a submit and an href and when I test the conditions submit works but the href does not and there is no error displayed . Please any help will be appreciated
Bellow is the code
<form action="test.php" method="post"/>
<a href="#" name="amhref" >Href</a>
<input type ="submit" name="button" value="button">
</form>
<?php
if(isset($_POST['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}
?>
a link <a> is not sent with a form. so any data you would like to pass should be done through the href field. this is done with a get method, so please note the $_GET
<form action="test.php" method="post"/>
<input type ="submit" name="button" value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>
<?php
if(isset($_GET['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}
?>
Using an <input> control to achieve this. I have used a hidden control.
<input type="hidden" name="id1" value="hello">
You can't do with an <a> tag
tag not element of form type so that you cant pass to in form submite. If you want to pass keep amhref value in input type text or Hidden.
You can pass value with href with GET method also.
<form action="test.php" method="post"/>
<input type ="submit" name="button" value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>
<?php
if(isset($_GET['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}

Categories