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>
Related
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 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
What is the best way to implement php code to run when
a button is pressed/clicked. Done some research but couldn't find anything relevant apart of few examples that show use of JavaScript.
I want it to be purely in PHP.
At this precise moment I have created the buttons in the following way <input type='button' value='Click Me'/> now not sure what to do. Could anyone help please.
I don't think you can run a PHP script by native HTML only. Using JavaScript would be the simplest solution you can do.
The basics of a form and php. when the button is pushed, the form calls an action (some page, even itself). Then the php checks if the button was pushed. If so, do something, if not, ignore it. Instead of creating a button though, a submit would work better, but the button works just fine.
<form method="post" action="./PHPScripts.php" />
<input type='button' value='Click Me' name='button' /> <-- added a name
<input type='submit' value='Click Me' name='submitButton' />
<input type='submit' value='Click Me' name='submitButton2' />
</form>
// below would be on the page called by the action in the above form
// which can be on the same page with no problem, but larger amounts of
// code become more difficult to read, so having multiple files is recommended
<?php
if($_POST['button']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton2']) {
// php code (either execute it, or call function)
}
?>
It's best to keep php code on top of html if on same page, but for demonstration reasons, I've put it on the bottom. (Easier to read)
The idea is, you will need to give the button the name attribute. Depending on how the form, is then submitted, in this case, to itself or to another page determined by the form action attribute and the method, the script will then run.
<form action="submit.php" method="post">
<input type='button' value='Click Me' **name="submit"**/>
</form>
if(isset($_POST['submit']) {
//runs when the button is clicked
}
<form action="index.php" method="post">
<input type='button' name='btn_func' value='Click Me'/>
</form>
<?php
if(isset($_POST['btn_func'])){
//call the function here.
}
?>
If you have 10 buttons:
<form action="index.php" method="post">
<input type='button' name='btn_func_1' value='Click Me 1'/>
<input type='button' name='btn_func_2' value='Click Me 2'/>
<input type='button' name='btn_func_3' value='Click Me 3'/>
<input type='button' name='btn_func_4' value='Click Me 4'/>
<input type='button' name='btn_func_5' value='Click Me 5'/>
<input type='button' name='btn_func_6' value='Click Me 6'/>
<input type='button' name='btn_func_7' value='Click Me 7'/>
<input type='button' name='btn_func_8' value='Click Me 8'/>
<input type='button' name='btn_func_9' value='Click Me 9'/>
<input type='button' name='btn_func_10' value='Click Me 10'/>
</form>
<?php
if(isset($_POST['btn_func_1'])){
//call the function here.
}
?>
Or use a Conditional/Switch statement.
If you are going to use input type="button", I am assuming you will need JavaScript to handle the click event? Then make an Ajax request to a PHP file or something?
Or, you could change your input type from "button" to "submit" <input type="submit" name="submitButton" id="submitButton" value="Submit" />
Then the...easiest (for lack of better words) is to check at the top of your page for the submit post variable. If it's there, PHP will handle it. Or, it will display the form if the variable is not set.
<?php
if (isset($_POST['submitButton']))
{
// form has been submitted.
// do something with PHP.
}
?>
<form action="file.php" method="post">
...
<input type="submit" name="submitButton" id="submitButton" value="Submit" />
</form>
I'm wondering how to resolve an issue where I have one text box and two buttons.
Each button needs the same data in the text box to accomplish its task.
One button is to update the existing record they are reviewing (with the new value in the text box), and the other button is used to add a new record (again, using the new value in the text).
One idea I had was to use jquery to update a hidden text box that gets updated when the visible text box is modified by the user.
So something like this: (this is just pseudocode...)
<form name="form1" method="post" action="controller1/method1">
<input type=text name=visibleTextBoxForForm1></input>
<button type=submit value=UPdate>
</form>
<form name="form2" method="post" action="controller2/method2">
<input type=hidden name=hiddenTextBoxforForm2></input>
<button type=submit value=New>
</form>
<script>
$('#visibleTextBoxForForm1').live('change', function() {
//update a hidden textbox in form2 with value of this textbox.
});
</script>
Is there a better way to do this?
Alternatively, you could do it via JQuery. Tie a clicklistener for each button and provide the correct URL to the form on click.
Here's some quick code... you'd have to correct the proper jquery queries for the correct elements.
<form name="form1" method="post">
<input type=text name=visibleTextBoxForForm1></input>
<button type=button value=Update>
<button type=button value=New>
</form>
<script>
$('update').click(function() {
$(form1).attr('action', <update url>).submit();
});
$('new').click(function() {
$(form1).attr('action', <new url>).submit();
});
</script>
If that's the only field, then simply have one form with two buttons and handle that text data based on the name of the button used to submit it.
<form name="form1" method="post" action="controller1/method1">
<input type="text" name="text" />
<input type="submit" name="insert" value="Insert New Data" />
<input type="submit" name="update" value="Update Existing Data" />
</form>
PHP (not CodeIgniter, since I'm not familiar with that framework):
if(isset($_POST['insert'])) {
// insert $_POST['text']
} else if (isset($_POST['update'])) {
// update $_POST['text']
} else {
// error
}
Using a form with a blank action - action="".
I have 2 buttons on the form that do different things. one to submit/save the info, the other to open an output sheet:
<input type="submit" name="SubmitSave" id="SubmitSave" value="Submit / Save" onClick="this.form.action='PA_Monitorcall.php'; this.form.submit()" />
<input type="submit" name="EmailDetails" id="EmailDetails" value="Email" onClick="this.form.action='OutputSheetPA.php'; this.form.submit()" />
I need the output sheet to open in a new window, but can't have this in the form header details, it will need to go in the code for the button above. Any ideas?
Cheers!
onClick event of both submit buttons, call a javascript function, which would toggle the 'target' attribute of the form tag to '_blank' or '_parent'/''.
with this new value for 'target' attribute your post would be submitted in a new window/tab
<form target="" action="" method="post">
<input type="submit" value="Same Window" onClick="ChangeTarget('same')" />
<input type="submit" value="New Window" onClick="ChangeTarget('new')" />
</form>
function ChangeTarget(loc) {
if(loc=="new") {
document.getElementById('form_id').target="_blank";
} else {
document.getElementById('form_id').target="";
}
}
Use type="button" instead. Your onClick already calls submit, so you don't need them to be submit inputs.
You can name the two inputs with the same name and then check the value of that inputs.
<form action="">
<input type="submit" name="submit" id="SubmitSave" value="Submit / Save" />
<input type="submit" name="submit" id="EmailDetails" value="Email" /></form>
</form>
And in the php file:
<?php
if ($_POST['submit'] == 'Submit / Save')
// save the form input
elseif ($_POST['submit'] == 'Email')
// do other stuff
...