Jump to content

Insert and Delete Button in 1 Form


yandoo

Recommended Posts

Hello there,

 

I was hoping for a little help with my form. It has 2 submit buttons, each with its own function (delete and insert)

 

I need to distinguish between which button is clicked....

 

Heres what i got:

 

if(isset($_POST['perform'])) {
foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes

	$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id. 
	$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}}

else if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['animalfearid'], "int"),
                       GetSQLValueString($_POST['animalid'], "int"),
                       GetSQLValueString($_POST['select'], "text"));

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<input type="submit" name="Submit" value="Submit" />
<input type="submit" name="perform" id="perform" value="Delete" /> 
}

 

I get wierd results when i run the page, the INSERT submit button works, but the DELETE Button strangley INSERTS a record instead?? So i dont think it is determing which Button is clicked...

 

How can i get this work propery? :)

 

Thank You

Link to comment
Share on other sites

use one form for each submit button like...

 

<form method="post" action="whateever">
<input type="hidden" name="id" value="someid">
<input type="hidden" name="action" value="insert">
<input type="submit" value="insert">
</form>
<form method="post" action="whateever">
<input type="hidden" name="id" value="someid">
<input type="hidden" name="action" value="delete">
<input type="submit" value="insert">
</form>

 

then grab the action and id(if applicable) from the hidden posted fields and process the request as such.

Link to comment
Share on other sites

Hi there,

 

Thaks for reply,. Is it possible to do it with just the one form though??? I At them moment the Delete button now works!!! However, it DELETES the record but also INSERTS a record too?????

 

I feel im close, if it is possible to do it in one form??

 

Thanks :)

Link to comment
Share on other sites

You should never rely on javascript for validation or detection...

 

In this case what you need to is very simple - check which button has been clicked...

 

Like the checkbox and radio button the submit button is only set when it is clicked - if it is not clicked then no value for that element is set.

 

So you have two submit buttons give them a name/id and then use that in the processing script..

 

the html...

<input type="submit" name="insert" id="insert" value="insert">
<input type="submit" name="delete" id="delete" value="delete">

 

the code...

<?php

if (!empty($_POST['insert']))
{
// do insert stuff.
}
else
{
// do delete stuff.
}
?>

Link to comment
Share on other sites

Hi there,

 

Thank you every body for replying and offering, help :)

 

i managed to get it working...I re thought what you orignally said about having 2 seperate forms and have found it to work a charm...

 

Was worried because i was using a query for BOTH forms and thought it may cause problems!

 

Additionally i was confussed about how to integrate the:

if(isset($_POST['perform'])) {

 

and the:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

 

And i can see how:

<?php

if (!empty($_POST['insert']))
{
// do insert stuff.
}
else
{
// do delete stuff.
}
?>

 

Will work too!! Ive really learnt a lot here :)

 

Thaks all for you help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.