Reputation: 1125
i have html form
i am using <a href="#" onclick="document.aa.submit()">
instead of submit button
my code will explain my problem better..
<html>
<form name="aa" action="aa.php" method="post">
<input type="checkbox" name="posts[]" value="1">
<input type="checkbox" name="posts[]" value="2">
<a href="#" onclick="document.aa.submit()">Delete</a>
<a href="#" onclick="document.aa.submit()">Move</a>
</form>
</html>
aa.php
<?php
print_r($_POST);
?>
results
Array ( [posts] => Array ( [0] => 1 [1] => 2 ) )
Now the question is:
How to know user clicked on delete or move?
i know if i used <input submit>
it will fixes the problem
but i can't use submit button for some reason
the question is how to detect it by php.
Example:
Array ( [posts] => Array ( [0] => 1 [1] => 2 ['submit'=>'delete']) )
if('delete'){
mysql_query("delete from...")
}
else{
mysql_query("update/move ....");
}
Upvotes: 1
Views: 276
Reputation: 1125
SOLUTION:
<script>
function createInput(action){
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "action");
input.setAttribute("value", action);
document.getElementById("forsm").appendChild(input);
document.aa.submit();
}
</script>
<html>
<form name="aa" id="forsm" action="aa.php" method="post">
<input type="checkbox" name="posts[]" value="1">
<input type="checkbox" name="posts[]" value="2">
<a href="#" id="delete" onclick="createInput('delete');">Delete</a>
<a href="#" id="move" onclick="createInput('move');">Move</a>
</form>
</html>
results
Array ( [posts] => Array ( [0] => 2 ) [action] => move )
Upvotes: 0
Reputation: 487
You need to make some tweaks to the form so the Post value knows which option you are submitting
<form name="aa" action="aa.php" id='aa' method="post">
<input type="checkbox" name="posts[]" value="1">
<input type="checkbox" name="posts[]" value="2">
<a href="#" onclick="submit_function(this)" rel="delete">Delete</a>
<a href="#" onclick="submit_function(this)" rel="move">Move</a>
<input id="submit_type" type="hidden" name="task">
</form>
in js with jquery
function submit_function( item ){
if( item.rel == 'move')
{
document.getElementById('submit_type').value ='move'
}
else if( item.rel == 'delete')
{
document.getElementById('submit_type').value ='delete'
}
document.getElementById('aa').submit()
}
in php
if( $_POST['task'] == 'move' )
{
//do move stuff
}
if( $_POST['task'] == 'delete' )
{
//do delete stuff
}
Upvotes: 0
Reputation: 1646
Here you go:
<html>
<form name="aa" action="aa.php" method="post">
<input type="hidden" name="action" value="">
<input type="checkbox" name="posts[]" value="1">
<input type="checkbox" name="posts[]" value="2">
<a href="#" onclick="document.aa.action='delete';document.aa.submit()">Delete</a>
<a href="#" onclick="document.aa.action='move';document.aa.submit()">Move</a>
</form>
</html>
Upvotes: 1
Reputation: 1430
With jQuery you can even get rid of the onclick
event in your tags and just add the following jQuery.
$(document).ready(function() {
$("a").click(function(event) {
alert($(this).html()) // Replace this line with the one below for your code
// document.aa.submit($(this).html())
});
});
The $(this).html()
will return the innerhtml of the click a
tag. e.g "Delete or "Move". You can then use this in your php to identify the clicked linke
Upvotes: 0
Reputation: 1582
Only PHP?... No javascript? If you are using javascript I would suggest the idea of using a button that calls some javascript to submit the form instead of a link, it might give you better results as you could also make some calls to set some hidden fields of the form before it is submitted. Also, why is submit broken?
Take a look at the following page for know how on how to use hidden fields.
http://www.tizag.com/htmlT/htmlhidden.php
upon post you should be able to get the hidden field as if the user had submitted it and then do your computation in the php instead of the html.
Upvotes: 1