Attached are pictures of my code. I am trying to pass the index.php value (either 4 or 8) of whichever button named 'num' is clicked to chooseSnacks.php and echo the variable $snacks.
The problem is that it only passes the last valued input in the form (which is 8) no matter if I click on 4 or 8. I reversed the order of the inputs just to double check, and when I did so I could only get the value 4 to pass no matter what I clicked.
Inputs on index.php
<div class="pricing-button">
<form method="POST" action="ChooseSnacks.php">
<input class="btn btn-primary btn-md btn-square" onclick="change('19.99')" type="text" value = "4" name="num[]" readonly></a>
<input class="btn btn-primary btn-md btn-square" onclick="change('25.00')" type="text" value = "8" name="num[]" readonly></a>
</div>
Submit button on index.php
<input class="btn btn-primary btn-lg btn-square" type="Submit" name="Submit" id="Submit" value='Submit'>
Session start on chooseSnacks.php
<?php
session_start();
$snacks = $_POST['num[]'];
?>
echo the variable
<div class="categories">
<h3>Your Carepackage</h3>
<p> <?php echo $snacks; ?> Snacks Every <?php echo $snacksNum; ?> </p> </div
In regards to $_POST['num[]']; you need to remove the [] from in there, because you already declared them as an array in name="num[]".
You then need to use a foreach($_POST['num'] as $var) and checking if they are not empty.
I used a ternary operator for that.
For example:
<div class="pricing-button">
<form method="POST" action="">
<input class="btn btn-primary btn-md btn-square" onclick="change('19.99')" type="text" value = "4" name="num[]" readonly></a>
<input class="btn btn-primary btn-md btn-square" onclick="change('25.00')" type="text" value = "8" name="num[]" readonly></a>
<input type="submit" name="Submit" value="Submit">
</div>
<?php
$inputs = !empty($_POST['num']) ? $_POST['num'] : array();
foreach($inputs as $value) {
echo $value . "<br>";
}
which will echo: (as tested)
4
8
I'm unsure as to what onclick="change('19.99')" etc. is supposed to react as though and the need for sessions where you started the session.
Well your input type is TEXT so it overrides the last value.
If you want the user to choose an option you should change the type to RADIO, OR set the value and name to blank and change it with javascript
Related
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" ...
I was wondering if it was in any way possible to be able to determine the ID of the form that was posted on PHP?
<form id="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
In the enroll.php
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
P.S: I'm not too sure on how i would do this on PHP. Thank you in advance
Post does not use the ID of the element, rather the name, so instead of your current form, you could use;
<form name="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<input name="formid" value="ES1A" /><!-- This holds your form id so you can use this -->
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
And in the PHP;
if (isset($_POST['ES1A']) // Unsure if form itself will be posted with the submit
{
// This is set as it uses the name of the element
$formid = $_POST['formid']; // Get the ID of the form from the element passed in
}
If form's name attribute does not work you can always set name for button:
<button name="ES1A" type="submit" class="btn btn-primary">Next Step</button>
Or:
<input name="ES1A" type="submit" class="btn btn-primary" value="Next Step">
PHP part should be the same as you already have:
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
an alternative is to use hidden input
<input name="ES1A" value="formid" type="hidden" />
I am trying to create a function that add up number to a given variable each time a button was click.
I have 4 buttons: farm, cave, house, casino
So what I am trying to achieve here is I need to send the random numbers generated by the buttons to a variable that will add up all of the SCORE on the "YOUR GOLD" section. So let's say I click the farm and the cave button so there will be 20 for the cave and 15 for farm for a total of 35 gold already.
Here's my form.php
<div class="wrapper">
<div id="gold">
<form action="process-game.php" method="post" >
YOUR GOLD: <input type="hidden" name="building" value="gold"/>
</form>
</div>
<div class="farm_form">
<h2>Farm</h2>
<form action="process-game.php" method="post" >
<input type="hidden" name="building" value="farm"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>Cave</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="cave"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>House</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="house"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>Casino</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="casino"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
</div>
Here's my process.php:
<?php
session_start();
if(isset($_POST['building'])){
echo earn_gold();
}
function earn_gold(){
if($_POST['building'] == "farm"){
$gold = rand(10,20);
}else if($_POST['building'] == "cave"){
$gold = rand(5,10);
}else if($_POST['building'] == "house"){
$gold = rand(2,5);
}else if($_POST['building'] == "casino"){
$gold = rand(0,50);
}
return $gold;
}
?>
Any idea how to do this?
I know, you basically wanted a solution in PHP. Still, I could not resist showing you, how easy it would be doing the same in JavaScript/jQuery. Have a look at it or simply ignore it. It is up to you ... ;-)
// define gold amounts for each button (min,max):
var finds={farm:[10,20],cave:[5,10],house:[2,5],casino:[0,50]};
$(function(){
$(':submit').click(function(){ // for all submit buttons: bind the click event to a function ...
var place=$(this).closest('div[id]').attr('id'); // get the id of the buttin's parent div
var fnd=finds[place]; // get the min/max array for the current button
with ($('#gold span')) // locate and use the <span> inside the div with id=gold
text(parseFloat(text()) // get the current value of the span (convert to float)
+fnd[0]+Math.ceil(Math.random()*(fnd[1]-fnd[0]))); // add the gold ...
});
})
div {display:inline-block; width: 120px; border:1px solid grey}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="farm_form" id="gold">
<h2>Your Gold</h2><span>0</span>
</div><br>
<div class="farm_form" id="farm">
<h2>Farm</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="cave">
<h2>Cave</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="house">
<h2>House</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="casino">
<h2>Casino</h2><input type="submit" value="Find Gold!"/>
</div>
If you really need it, I guess you can do it with sessions.
if (isset($_POST['building'])) {
// getting random gold
$earn_gold = earn_gold();
// adding to session called "gold"
$_SESSION['gold'] = (isset($_SESSION['gold']) ? $_SESSION['gold'] + $earn_gold : $earn_gold);
// redirect back to process page
header('Location: process.php');
exit;
}
And then outputting it like so
<div id="gold">
<form method="post" >
YOUR GOLD: <input type="hidden" name="building" value="gold"/>
<?php
// if set, outputting sessions "gold" value
if (isset($_SESSION['gold'])) echo $_SESSION['gold'];
?>
</form>
</div>
When the user click on find gold then submit a form which will add the gold to the total result. For example if the user gets 3 gold then add the gold variable.
$totalGold = 0;
when user.click button then $totalgold +3;
Have one form element and use javascript to assign onclick events to each of the butons and may be use ajax to submit the form dynamically and display the results back on same page.
I need the button text displayed to be different from the sent form value.
Is there a way to achieve this using CSS|HTML|PHP? Here's my pseudo.
<form method='post' action='index.php'>
<input type="submit" value="name1" name="remove">Remove 1</input>
<input type="submit" value="name2" name="remove">Remove 2</input>
<input type="submit" value="name3" name="remove">Remove 3</input>
</form>
<?php
#Pressing Remove 1 will print "name1", Remove 2 will print "name2", etc.
if(isset($_POST['remove']))
{
$_gone = $_POST['remove'];
print $_gone
}
?>
Use a button element (which can have child nodes), not an input element (which cannot).
<button type="submit" value="name1" name="remove">Remove 1</button>
Note that old IE has limitations here.
I have a form with multiple submit buttons.
Each submit button is an IMG SRC trash can which denotes the delete icon for messages in a web based messaging mail inbox
what is the best way to figure out which submit button icon was clicked so that I can then write the PHP/MySQL code to DELETE the message?
if(!empty($_POST)){
// How do I figure out which submit button has been clicked to get the ID of the message to delete?
}
<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
Set value for each submit button and check that in php and find which one is clicked
<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>
echo $_POST['submit_btn']; will give you the value of which submit button is clicked
Give each button a name=""
Then you can do something like
isset($_POST['button_name']) {
// execute code here if true
}
THE solution of this problem is to use the NAME attribute of the tag input/button.
<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>
or
<button type="submit" name="submitSave">Save</button>
<button type="submit" name="submitAddComment">Add comment</button>
I think you can also use the value attribute of button tag, this is definitively not possible with input tag.
If you need to use an ID or another variable, use name="submitDelete[888]"
Then, check it with PHP:
if( isset($_POST['submitDelete']) ) {
echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
}
So many years later, I like button because it allows to display a text or an image independently of the value returned.
Here is an illustration of possibilities which fits the title of this post and more cases than the OP.
<?php
if(!empty($_POST['id'])){
echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
echo 'Default clicked'; // ?
}
?>
<form method="POST">
<!-- Original Post examples -->
<button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
<button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
...
<button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>
<!-- Additional possibilities -->
<!-- ?action=create -->
<button type="submit" name="action" value="create">New element</button>
<!-- ?action -->
<button type="submit" name="action">Refresh</button>
<!-- ? -->
<button type="submit">Default</button>
</form>
you can give a name and a value to each of your buttons. It will then show up under $_POST['submit']
<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />
You have to pass your value to the current file by declearing name and value for each.. then you can echo in your php script in order to know which one is clicked.