Two buttons in a form with different functions - php

I just wanna ask how can I make a form with two buttons that do different functions? Like for example: I have this table with checkboxes and when a checkbox was checked and a certain button was clicked, the button will perform its assigned task.
Button1 can add, button2 can delete.
Can you help me again, please? I'm kind of new at this and I really want to know. Thank you!

This is more of a JavaScript question than PHP.
You'll need to add an onclick to your buttons:
<input type="button" onclick="functionA();" value="button a" />
<input type="button" onclick="functionB();" value="button b" />
Then create these functions in JavaScript:
function functionA()
{
// do stuff
alert("add");
}
function functionB()
{
// do stuff
alert("delete");
}
Here's a basic example of where you can stick your JavaScript etc:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<script type="text/javascript">
function functionA()
{
// do stuff
alert("add");
}
function functionB()
{
// do stuff
alert("delete");
}
</script>
</head>
<body>
<input type="button" onclick="functionA();" value="button a" />
<input type="button" onclick="functionB();" value="button b" />
</body>
</html>

If you are looking at a server side solution, you can have two submit buttons in the form with different name attributes and then check the $_GET['buttonName'] or $_POST['buttonName'] variables depending on your form submission method.
For example:
<form action="action.php" method="post">
<input type="text"....blah blah />
....
<input type="submit" name="add" value="Add" />
<input type="submit" name="delete" value="Delete" />
</form>

Related

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

onclick input type submit

In a form I have an <iframe> that contains a PHP file (editor.php). This PHP file contains an HTML form.
Well, when I do a "submit form", I call the same PHP file, for example main.php.
When I press the submit button, I have "onclick method" that it calls a Javascript function inside editor.php. This function executes the form.
My problem is that main form is executed correctly but the second form is not.
In the second loop of the form of editor.php receives nothing.
**Check this way it will work as you expected**
//main.php
<html>
<head>
<script type="text/javascript">
function validateMain()
{
alert('main');
}
function validateSub()
{
alert('sub');
}
</script>
</head>
<body>
<form id="main" onsubmit="return validateMain();">
<input type="text" name="first" id="first"/>
<input type="submit" value="Submit Main"/>
</form>
<iframe name="ifr-form" id="ifr-form" src="test.html"></iframe>
</body>
</html>
//test.html the second form included through iframe
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form id="sub" onclick="return window.top.validateSub();">
<input type="text" name="first" id="second"/>
<input type="button" value="Submit Sub" />
</form>
</div>
</body>
</html>
you must check if php and iframe are compatible, as far as i Know I don't think that frames and php gives any output, hope this helps.

Submit button clicking changing the value of it to another thing

maybe very easy!
I'm php coder and I don't have experience in js but I must do this for one of my codes
suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<?php
//some code
if(array_key_exists('update',$_POST)){
//somecode
}
?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
</form>
</body>
</html>
show as function name does not really make sense here (imo), but you could do:
<input type="submit" name="sub" value="sub1" onclick="show(this)">
and
function show(element) {
element.value = 'sub2';
}
Important:
But that will actually not solve your problem. As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page. So every change you made the current page is lost anyway.
The question is: What are you trying to do?
It seems to me that you should change the value of the button on the server side. You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.
Update:
I see several possibilities to solve this:
You could keep using JavaScript and send and get the data via Ajax. As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.
You could add a GET parameter in your URL with which you can know which label to show for the button. Example:
<form method="post" action="?update=1">
and
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
Similar to 2, but use a session variable (and not a GET parameter) to keep track of the state.
Update2:
As you are already having $_POST['update'] you don't need the URL parameter. It could just be:
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
</form>
</body>
</html>
This should do it
function show(){
document.getElementsByName('sub')[0].value = 'sub2';
return false;
}
Edit: if you don't want it to submit the form, just add a return false, but then you'd need to change your onclick from your submit button to your forms onsubmit;
<html>
<head>
<title>test</title>
<script>
function show()
{
document.getElementById("sub").value= "sub2";
return true;
}
</script>
</head>
<body>
<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>

Javascript autosubmit form

I have a form that I need to submit automatically... (the fields are already filled and its complicated to explain why but it IS necessary to do it this way)..
I know how to autosubmit the form using Javascript but the only problem I have is that there is more than 1 submit button.. and I need 1 in particular to be submitted...
thanks in advance
EDIT2(source):
<I put the javascript in the head... />
<FORM ACTION="PDF.php" name="form" METHOD="post">
<A whole bunch of inputs />
<INPUT TYPE="submit" name="form-save" VALUE="Save Changes" >
<INPUT TYPE="submit" name="form-submit" VALUE="Submit" >
<input type="submit" name="print" id="print" value="Download PDF" />
</form>
instead of going for a click event on a submit button, you can call submit of a form object from javascript.
Example :
<head>
<title>Auto Submit Form</title>
<script type="text/javascript">
window.onload = function () {
var form = document.getElementById("PDFGenerationForm");
form.submit();
};
function OnFormSubmit() {
alert("Submitting form.");
}
</script>
<body>
<form id="PDFGenerationForm" action="" method="post" onsubmit="OnFormSubmit">
<!--Any input tags go in here-->
</form>
This editor won't let me paste the whole HTML in here. So, it is in fragments.
$("#yourbuttonid").click();
EDIT:
<form>
...
<input type="submit" id="myFirstsubmit" />
<input type="submit" id="mysubmit" />
</form>
<script type="text/javascript">
$(document).ready(function(){$("#mysubmit").click();});
</script>
If you really want to click a specific button, add this script to the end of your page:
<script type="text/javascript">
// press the button
var myButton = document.getElementById("idOfTheButtonToClick");
myButton.click();
</script>
This assumes your button has an ID.
1) Here is a working auto-submit method: when page is loaded, the form will be immediately autosubmited (the values can be set with php variables too.)
<form action="page.php" name="FORM_NAME" method="post">
<input type="text" name="example1" value="YOUR_VALUE" />
<input type="submit" />
</form>
<SCRIPT TYPE="text/JavaScript">document.forms["FORM_NAME"].submit();</SCRIPT>
or use for any form on that page:
document.forms[0].submit();
2) you can use button-click (called after 1 second):
<SCRIPT TYPE="text/JavaScript">setInterval(function () {document.getElementById("myButtonId").click();}, 1000);</SCRIPT>

jQuery manipulating forms

For some reason it's not changing the action of the forum. I have some code to change the action of a form when a button is clicked:
function changeForm(event){
alert("Before: "+jQuery("#franchiseform").attr("action"));
jQuery("#franchiseform").attr("action", "franchisepreview.php");
alert("After: "+jQuery("#franchiseform").attr("action"));
jQuery("#franchiseform").submit();
}
The binding:
jQuery("input.preview").bind("click", changeForm);
The form:
<form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
The buttons:
<input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" /><input type="submit" value="Insert" />
First off, you need to clean up the
<input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" />
to:
<input type="button" value="Preview" id="preview" name="preview" />
as an input should not have multiple id attributes and you should not use classes and id's with the same name. This could be one reason why you're having problems. Then I used the following code and the action url was actually changing:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#preview").bind("click", changeForm);
function changeForm(event){
alert("Before: "+ $("#franchiseform").attr("action"));
$("#franchiseform").attr("action", "franchisepreview.php");
alert("After: "+ $("#franchiseform").attr("action"));
$("#franchiseform").submit();
}
});
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
<input type="button" value="Preview" id="preview" name="preview" />
<input type="submit" value="Insert" />
</form>
</body>
</html>
Unless you stripped out too much, it looks like you need to put your "after" call into a function called "changeForm", which is what you are binding to the click event.
otherwise, keep chaining:
jQuery("input.preview").bind("click", function(){
jQuery("#franchiseform").attr("action", "franchisepreview.php"));
jQuery("#franchiseform").submit();
});
It turns out that this is a Firefox / Mac specific bug. It works on other platforms and browsers, but not this specific computer for some reason. It could be an extension conflict or something else, I'll simply use another browser.

Categories