Jump to content


Photo

Text Link As Submit Button


  • Please log in to reply
3 replies to this topic

#1 Eiolon

Eiolon

    Advanced Member

  • Members
  • PipPipPip
  • 353 posts

Posted 28 December 2012 - 02:58 PM

I am going to guess this is a problem with javascript, because I can use a typical submit button and have it work.

I am trying to make it so a text link acts as the submit button and have it delete records that are selected via checkbox.

When I click the delete items button it just refreshes the page and removes the check from the box. The record is still there. If I use a submit button it works.

I have for the link:

<a href="#" onclick="document.forms['delete_items'].submit();" title="Delete Items">Delete Items</a>

I have for my form:

<form id="delete_items" name="delete_items" method="POST" action="">

	<table class="data">
	 <tr style="background:#F1F1F1;">
	 <td style="width:20px"></td>
	 <td>Item</td>
	 <td>Owner</td>
	 <td style="width:80px">Received</td>
	 <td style="width:80px">Returned</td>
	 <td style="width:80px">Method</td>
	 </tr>
	 <?php while ($row_returns = $sth_returns->fetch(PDO::FETCH_ASSOC)) { ?>
	 <tr onmouseover="this.bgColor='#EBFAFF';" onmouseout="this.bgColor='#FFFFFF';">
	 <td><input type="checkbox" name="<?php echo $row_returns['id'] ?>" value="<?php echo $row_returns['id'] ?>" style="border:none;" /></td>
	 <td><?php echo htmlspecialchars($row_returns['item'], ENT_QUOTES, 'UTF-8'); ?></td>
	 <td><?php echo htmlspecialchars($row_returns['owner'], ENT_QUOTES, 'UTF-8'); ?></td>
	 <td><?php echo htmlspecialchars($row_returns['date_received'], ENT_QUOTES, 'UTF-8'); ?></td>
	 <td><?php echo htmlspecialchars($row_returns['date_returned'], ENT_QUOTES, 'UTF-8'); ?></td>
	 <td><?php echo htmlspecialchars($row_returns['method'], ENT_QUOTES, 'UTF-8'); ?></td>
	 </tr>
	 <?php } ?>
	</table>

</form>


 

I have for my PHP script:

if ($_POST['id'] ) {
foreach($_POST as $id) {
$sth_delete_item = $dbh_mysql->prepare('
DELETE
FROM returns
WHERE id = :id
');
$sth_delete_item->bindParam(':id', $id);
$sth_delete_item->execute();
header("Location: index.php");

}
}



Edited by Eiolon, 28 December 2012 - 02:59 PM.


#2 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 28 December 2012 - 03:51 PM

From what I can see, you are trying to delete $_POST[id] but none of your checkboxes is named "id". If you view the source code (in your browser), you'll probably see the name as a number. Also, some servers don't pass arrays from your HTML form and you'll only get a string value "Array", you might want to check that as well.

NoGray.com


#3 Eiolon

Eiolon

    Advanced Member

  • Members
  • PipPipPip
  • 353 posts

Posted 28 December 2012 - 04:15 PM

Thanks, problem solved!

#4 Adam

Adam

    Advanced Member

  • Gurus
  • 5,685 posts
  • LocationSheffield / UK

Posted 29 December 2012 - 07:00 PM

Have you considered styling a submit button as a link by the way, to avoid the JS dependency?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com