Sending delete action as POST request - php

I have a datagrid with bulk actions to delete as well as single delete.
Here is the snapshot of my datagrid:
my datagrid is as follow :
<form method="post" action="">
<table>
<tr>
<td><input type="checkbox" value="1" name="checkIds[]"/></td>
<td>user</td><td><img src="delete.png"/></td>
</tr>
</table>
</form>
I can submit data as post when using bulk action delete. Currently for single delete using the delete button action as you can see in the Action column, i am just putting hyperlink or sending it as GET request. As i came to know, the delete action should use POST or DELETE to avoid web crawlers accidentally deleting my data. I thought of a solution to send it as POST action as follow:
<form method="post" action="user/delete/1" id="form_id_1"></form><img src="delete.png"/>
Problem is that this form tag will be inside my parent form tag and it violates html standards since form tag cannot be inside another form tag
How can I send post request for single delete action here ? Any logic please?

Try:
<form method="post" action="">
<table>
<tr>
<td><input type="checkbox" value="1" name="checkIds[]"/></td>
<td>user</td><td><input type="submit" class="sdelete" name="sdelete" value="<?= $ID; ?>" /></td>
</tr>
</table>
</form>
From there, you would use css to modify how the .sdelete looks, and in PHP you can get the single delete by:
if(isset($_POST['sdelete'])) {
$id = $_POST['sdelete'];
//use $id to delete a single entry from DB, and then produce new output
}
And that should work flawlessly. ^^

Don't add another form. Just add another submit button. Use the same form handler, and have it check to see if any of the "delete me" submit buttons are in $_POST (which will only happen if they are clicked).
You'll have to use POST for all your actions though.

I came of with a solution as below . please correct me if i m doing it wrong
<form method="post">
<table>
// datagrid
<img src="delete.png"/>
</table>
<input type="hidden" value="" name="id"/>
// to distinguish between single delete and bulk delete
<input type="hidden" name="single_delete" value="" id="single_delete_flag"/>
</form>
//js
//onclick handler for anchor action delete:
$("#id").attr("value",$(this).attr("data"));
$("#single_delete_flag").attr("value","1");
$("form").submit();
//delete.php
check if request if post
check if single_delete is 1, then single delete
else if check bulk delete

Related

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

How do I submit my form using href?

Am using this onclick='document.forms['form_name'].submit(); return false;' but this doesn't work as am having href=results.php?page_no=1 etc, has dynamic links, examples show to make this work I need to use href="#" but any idea how I can submit the form and than navigate to page2? Because I want to save the check box values in a session
Add class to your hrefs (class="pagination") and id (id="form") to your form. Then you can use Jquery framework for this stuff.
$(".pagination").click(function(){
// get page id, set form action with params
$("#formId").submit();
return false;
});
You have a bad design.
You can't perform multiple competing actions on a form click and expect it to work.
You need to either let the link be clicked and let it load another page, or if you are just setting some session variable (although it would be far better to set this with a querystring parameter or a cookie), you can use an Ajax request to send that off asynchronously.
Here I substituted page2 with Google just for test
Submit
<form method="get" action="https://www.google.com/search?q=test" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
edit:
Submit
<form method="get" action="" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
without encodeURIComponent(this.getAttribute('href') the parameters are missed.
Some options:
1) Use jQuery AJAX, serialize and post the form data and then redirect (location.href) on the onSuccess callback. Something like this:
$.post("submitform.php",
$("form").serialize(),
function(data){location.href='results.php?page_no=2';}
);
2) Post the form to a named hidden iFrame using "target" on the form tag. If this is really just a best effort sort of recording you shouldn't need to wait for the page to load, the request should be enough and you can continue to the next page. Something like this:
<iframe="targetname" style="display:none;" />
<form name="myform" target="targetname" method="post" action="submitform.php">
....
</form>
<a href="page2.php" onClick="document.forms['myform'].submit(); return true;">
Click Here
</a>

Why won't jQuery submit this form?

I am trying to submit a form with jQuery and I must be missing something small, because I can't get this to work, and from everything I see it should work fine.
What's wrong with this?
<table class="newrecord"><form id="editthis" action="page.php" method="post">
<tr><td class="left">Name:</td><td><input type="text" name="name" id="name" /></td></tr>
<tr><td class="left">Company:</td><td><input type="text" name="company" /></td></tr>
<tr><td class="left">Cancel</td><td><input type="button" name="submit" class="subbut" id="subthis" value="Update" /></td></tr>
</form></table>
And the javascript:
$("#subthis").click(function() {
$('#editthis').submit(); // An alert box works, so I know this is triggering
});
As mentioned in the code, an alert box works if I click the submit button, but when I use the jQuery submit function, nothing happens. What am I missing???
You don't need to use jQuery to submit a form. It's default behavior for a submit button to submit the form it belongs to.
Also, don't use a table for layout. The form elements themselves can layout just fine.
<form id="edit_this" action="page.php" method="POST">
<label>Name <input type="text" name="name"></label>
<label>Company <input type="text" name="company"></label>
Cancel
<button type="submit">Update</button>
</form>
Can be easily layout'd and will submit on its own.
If you need something to happen before the submission with jQuery, bind it to the form's onsubmit handler, rather than the actual click of the button.
The actual problem is the collision between the name you've given to the button and the reserved word in JavaScript. Don't use submit as the name.
I see two possible problems.
1) You have form tags inside table tags. While this probably isn't the root cause of your problem, it's not valid HTML.
2) You've used "submit" as the name of your submit button. This should be avoided because your object will collide with JavaScript reserved words. Use something other than "submit" like you've done with the id attribute.

php forms, how to use multiple forms in one page?

i have 2 or more forms in one page. like this:
<form method="post" >
<table border="0">
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="submit" value="get" />
</form>
<form method="post" >
<table border="0">
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="submit" value="get1" />
</form>
the forms are similar but with different content.
what happens is when i click the get button on one form, all other forms get triggered also.
is there a way to differentiate the forms so that when i click on a button only the form that has that button to get triggered?
edit: if i press on get1 the form with get gets also triggered
thanks
Only the form which contains your submit-button should be triggered. Are you sure you closed both forms properly in the HTML?
Can you post a link to the page or paste all the HTML?
If your code is set like...
if(!empty($_POST)) {}
then it'll seem like both forms are submitted, but really you can only access data from the form that was actually submitted. If you put a name on the two submit buttons you can test for $_POST['submitButtonName'].
You can use this page to see the differences pretty easily.
http://pastie.org/pastes/2290937/text
You can't submit more than one form, your first form is just probably wrongly closed, which causes you a trouble
Hope this code will help you.
If you want to submitt particular
form use javascript or jquery.
In my case i will choose jquery.
Here is the example of my solution.
You will need to change formid and objectid.
$( "#buttonid" ).click(function() {
$( "#formid" ).submit();
});
The short answer is you really only have one form on your page.
You should give each of your forms a name and id to uniquely identify them.
<form name="form1" id="form1" method="post" action=""> is the way to make each of your forms different. It is important because you may want controls other than a submit button to submit the form. Consider this code in the OnChange event of a dropdown list:
"document.forms.form1.submit()"
This will make the form submit as soon as a value is picked in the dropdown list. For advanced features to work, you need to know what your form's name is. The id for the form can be used to access it using document.getElementById('form1')
Finally, do not use tables to display your forms. Use CSS.
I hope this helps someone.

Multiple forms and one PHP receiver

I have MySQL generating forms on one page with the same action and submit button. The number of forms vary. They all call the same PHP file when submitted. Also, I have one PHP file which collects the data upon submission. See the example below.
The problem is when one of the forms is submitted, values get confused with different fields from different forms. Example: When form1 is submitted, PHP receives the form6's values.
How can I make sure each form submits its own values?
HTML Code Example:
<form method="POST" action="index.php?action=newhistory" name="history_1">
<input type="hidden" name="id" value="1">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
<form method="POST" action="index.php?action=newhistory" name="history_6">
<input type="hidden" name="id" value="6">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
PHP Code Example:
case 'newhistory':
$id = $_POST['id'];
$history = $_POST['history'];
$sql = mysql_query("INSERT INTO history (id, history) VALUES('".$id."', '".$history."')", $link);
break;
Any solutions?
It will submit the content encapsulated by the <form></form> tags. Having several form's action attribute point to the same page should not create the problem you describe.
The code you wrote here looks fine. Check your HTML code, and ensure you have the corrent <form></form> tags surrounding the elements of each form.
Please use different names for the submit button, then check.
if(isset($_POST('add_history'))
{
}
if(isset($_POST('add_history1'))
{
}
If you hit submit in one form, the browser is supposed to send only the values from this form. When form1 is submitted, PHP will NOT receive the form6's values. Maybe you didn't close the form tag properly or have any JavaScript going wild.

Categories