php check the button is set and show some message - php

I have a markup like this
<button type="button" class="btn btn-success pull-right add-customer" name="add-new-customer">Add New Cusomer</button>
so when the add new customer will be clicked it should show some message. like this
<?php
if(isset($_POST['add-new-customer'])) {
echo 'Set';
}
?>
but it is not doing isset for the button. So can somone tell me how to solve this using php. I have not used any kind of form. I want to simply check the button is set and show some message. Any help will be really appreciable. Thanks

This is because if you click a button within a form it doesnt actually submit the form.
Try using
<input type="submit" value="Submit" name="add-new-customer" />
EDIT:
Having just seen your comment, you must wrap the elements in a
<form>

Button elements aren't linked to anything in HTML. PHP won't detect if you click on a button.
You have to use a form or an AJAX query in order to populate the $_POST variable.
<form action="" method="post">
<input type="submit" value="Submit" name="add-new-customer" />
</form>

button type must be submit like type="submit" not type="button"
<button type="submit" class="btn btn-success pull-right add-customer" name="add-new-customer">Add New Cusomer</button>
also use form tag

Related

A way to determine ID of form during POST method

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

How to submit form data in bootstrap for PHP?

I am currently using PHP form element : the submit button as
<input type="submit" name="submit" value"set"/>
and the form was being submitted like :
if(isset($_POST['submit']) && $_POST['submit'] == 'set'){..do something..}
Now in bootstrap a submit button is usually written as
<button type="submit" class="btn btn-success btn-sm">set</button>
My question is how to submit this form data since I cannot do it like the previous way. It might be a noob question but I am new to bootstrap. Any suggestions would be of great help.
This has little to do with bootstrap. You can set a name and value attribute on a button as well as an input[submit].
<button type="submit" class="btn btn-success btn-sm" name="submit" value="set">
set
</button>

Multiple button on a single form but only one submit button?

I want to pass data from a single form, but it has another 2 button to add and delete questions, the problem is when I click on any of problem or create a question problem, the form problem to the action url.. I just want when I click on the Submit button then action is activated.
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
The default type of a button element is submit.
You can specify type="button".
It is better practise, however, to give it a name and a value so that you can perform whatever task you want it to do server side should the JS fail, and then call event.preventDefault() in the event handler function.
I think I have found the solution instead of using
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
I have to use this
<form method='POST' action='scripts/generateguide.php'>
<input name='create question' class='addP' type="button" value="Submit">
<input name='delete' type="submit" value="btton" class='deleteCon'>
<input name='createGuide' type="submit" value="Submit">
</form>

how to check which button pressed in php

i got following code i want to know which button pressed then pass the value to input box.
<button type="button" name="buttonpassvalue" value="1" onclick="">Value1</button>
<button type="button" name="buttonpassvalue1" value="2" onclick="">value 2 </button>
<?php
if buttonpassvalue pressed then add the buttonpassvalue value
<input type="text" name="value">
else
add value of buttonpassvalue1
?>
i am tried to solve but stock here.
please help me
thanks
The best way to do this is with Javascript.
As PHP is a server side language, it requires you to send some information to the server, meaning you would have to submit the form, and reload the page with the details of the request from the user.
With a javascript library like jQuery you can do something like the following.
<button class="some-button" value="1">Button 1</button>
<button class="some-button" value="2">Button 2</button>
<input type="hidden" name="buttonValue" class="button-value-hidden" />
<script>
$(document).ready(function(){
$('button.some-button').on('click', function(){
$('input.button-value-hidden').val($(this).val());
});
});
</script>
Now $_GET['buttonValue'] will contain your button value when the form is submitted.
Make sure you are including the jQuery library!

How do I get the value of a button element when submitted with PHP?

With <input type="submit"> it's easy enough, it's just the value attribute. But for a <button>Value</button> the value goes in the middle (this allowed me to have an icon in the button label which was important).
How do I get that in PHP? If my button looks like <button type="submit">Text <i class="icon-remove"></i></button> do I include the <i></i> part?
Just give your button element a name and value with the data you want passed.
<button type="submit" name="button" value="whatever">Text <i class="icon-remove"></i></button>
Assuming that your form use method POST:
<form method="POST" action="">
You could define your button like that:
<button value="value_button" type="submit" name="name_button">Submit</button>
And get his value like that:
$valueButton = $_POST['name_button'];
<button name="name_of_button" value="some_value">Some Value</button>
assuming your form is POSTed the value is then used like $_POST['name_of_button']

Categories