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>
Related
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
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>
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 "state" of buttons which have been pressed in bootstrap?
I.e. I am using this code, which makes buttons 'toggle' like a checkbox:
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
Which is from the bootstrap manual here.
But which I then click a submit button - there is no data contained in the $_POST. How do I know which button(s) have been selected?
I have tried adding 'value="1" name="1"' etc aswell to the buttons - but still nothing.
Edit: Here is the full solution, which I was able to create using the help from Felix below.
<form method="post" action="" id="mform" class="form-horizontal">
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" name="left" value="1" class="btn btn-primary">Left</button>
<button type="button" name="middle" value="1" class="btn btn-primary">Middle</button>
<button type="button" name="right" value="1" class="btn btn-primary">Right</button>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<script>
$('#mform').submit(function() {
$('#mform .btn.active').each(function() {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", this.name);
input.setAttribute("value", this.value);
document.getElementById("mform").appendChild(input);
});
});
</script>
Buttons that are "selected" will have the active class attached to them. You can then use jQuery to only select those buttons. Since buttons are form elements, they can have name and value attributes and it's probably best to make use of those.
Example:
var data = {};
$('#myButtonGroup .btn.active').each(function() {
data[this.name] = this.value;
});
To send the data to the server, you'd could use Ajax and set the data manually / merge it with other form data (if it exists).
You could also try to mirror the values into hidden form elements on submit if you want to use "traditional" form submission (see Change form values after submit button pressed).
I have a form with action="autosave.php". This saves as normal when enter is clicked or the submit button is pressed.
<input type="submit" id="save" value="Save Changes" />
I am also using jquery autosave, which automatically submits the form every 10 secs
$('form.checklist').autosave({interval:10000,......});
My problem is, i want to put jquery validation on the form, but i dont want it to validate for any any the above 3 processes.
I only want the validation to take place when a button is clicked.
<button class="ui-state-default ui-corner-all" onclick="location.href='submit.php'" type="button">Submit</button>
Any help would be appreciated.
Thanks in advance
<button id="submit" class="ui-state-default ui-corner-all" type="button">Submit</button>
<script>
$('#submit').click(function(e){
dovalidation (obviously this must be your validation function)
if(validation==true){
location.href='submit.php';
}else{
e.preventDefault; return false;
}
});
</script>