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" />
Related
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 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.
This is my problem - when I hit the submit button it is supposed to execute some code. Unfortunately all it does is refresh the page at a weird url using the submit's ID and value.
File is called Q2C.php.
This is the code:
<?php
if(isset($_POST['submit'])) {
$arraylength = count($selectedQuotes);
for($i = 0; $i < $arraylength; $i = $i + 1) {
$supplierQuoteID = $selectedQuotes[$i];
$updateSQL = "UPDATE supplierquotes SET `Selected` = 1 WHERE `SupplierQuoteID` = '$supplierQuoteID'";
$updateQuery = mysql_query($updateSQL);
}
}
?>
<div class="buttons">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</div>
It redirects me to this url: (path)/Q2C.php?submit=Create+Q2C
How can I stop it from redirecting me here, and instead just perform what is in the if isset statement?
You need to put your buttons into a form.
<form method="post">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</form>
You should consider changing your second submit button to a cancel button.
I don't see a <form> in your HTML, but you need to be using POST, not GET
<form method=post>
You have not submitted your form completely in the above code, but it is clear from the redirected url that you are using GET as post method of the form. And in your isset check you are using $_POST. Change the form method to post then it will work fine.
You need to specify method. By default its get
<div class="buttons">
<form action="Q2C.php" method="post">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</form>
</div>
You should probably use ajax to submit a form if you don't want to refresh the page. Here is the same code you can refer to:
$.ajax({
type: "GET",
url: "abc.php",
data: "name="+form.name.value,
success: function(rs)
{
// do success stuffs here
}
});
Hello am not sure if i get your problem correct its like You want Submit a form to Q2C.php with out a form attribute called "Method" if u dont set the method at all, the form will use the GET method as default..... but from your code it seems the form is using get to submit to Q2C.php.... and you are saying (isset($_POST['submit'])) which is wrong because the Submit button was never set.. what u need to do is change set the attribute method of the form to "post" method and try running the code again... your form should look like this...
.........
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.
I have a doubt on the following code. My function is not called when the save button is clicked .
This is the following code for save function,
if(isset($_POST['Save'])) // If the submit button was clicked
{
echo "hgfd";
$post['ProductSegmentCode'] = $_POST['ProductSegmentCode'];
$post['ProductSegment'] = $_POST['ProductSegment'];
$post['ProductGroup'] = $_POST['productgroup'];
// This will make sure its displayed
if(!empty($_POST['ProductSegment'])&&!empty($_POST['ProductSegmentCode'])&&!empty($_POST['productgroup']))
{
echo "SAVE";
$news->addNews($post);
?>
<script type="text/javascript">
alert("Created Sucessfully..!!");
</script>
<?
}
else
{
?>
<script type="text/javascript">
alert("Enter Mandatory Fields");
</script>
<?
}
}
following is the button format in html,
<div style="width:70px; height:32px; float:left; margin-top:16px; margin-left:4px;">
<input name="Save" type="button" class="button" value="Save">
</div>
Your button is type="button"; to get the form to submit, it needs to be type="submit". Try updating it with this and it should work (also pending you form has action="post", or no action specified; the default is post):
<input name="Save" type="submit" class="button" value="Save" onclick="Save" />
Also, you're using onclick="Save" in your button. This indicates you have a corresponding JavaScript function named Save() - though, per your code examples you do not show one. I'm assuming that this is in error and can safely be removed (the value="Save" can also be removed as you only need to check isset($_POST['Save']) and not it's actual value). All changes in-place should give you:
<input name="Save" type="submit" class="button" />
If you do, in fact, have a JavaScript function named Save(), please post its code and I can revise.
use form for sending data and use type submit
<form action="" method="post">
<input name="Save" type="submit" class="button" value="Save">
</form>
and if you want to use this
<input name="Save" type="button" class="button" value="Save" onclick="Save()">
create Save() function in javascript and use ajax call for sending data.
It looks like you should change
<input name="Save" type="button" class="button" value="Save" onclick="Save">
to a summit button.
<input name="Save" type="submit" class="button" value="Save">