passing actionable info...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

passing actionable info...

Post by arpowers »

Hey guys,

I would like to add a 'delete field' button to a page I am working on...

first thing that comes to mind is using $_GET to pass the info back to the same page. ... self.php?a=delete and then do the required action...

I know this is not recommended.. what is the best way to pass the info?
User avatar
gregsometimes
Forum Newbie
Posts: 11
Joined: Sun Oct 21, 2007 1:57 pm
Location: San Francisco, CA

Post by gregsometimes »

There isn't a "best" way for one specific task in particular. It depends on the context. For example do you want your site to be secure? or does speed of execution matter to you? Anyway, this is just theory.

Have you considered using $_POST instead of $_GET?

You can pass parameters from within a form, with a method="post" in hidden input fields:

<form action="landingpage.php" method="post">
<input type="hidden" name="x" value="1">
</form>

and then picking up with $var = $_POST['x'] on the landing page

For multiple fields, you can have multiple hidden fields, or simply enumerated checkboxes (say with similar names like "delete1", where 1 is the mysql key id number of the field) and scan through them on the landing page with PHP, then parse the values.

example:

Code: Select all

<form action="landingpage.php" method="post">
<input type="checkbox" name="delete_mysqlid71"><br>
<input type="checkbox" name="delete_mysqlid73"><br>
<input type="checkbox" name="delete_mysqlid74"><br>
<input type="checkbox" name="delete_mysqlid75"><br>
<input type="checkbox" name="delete_mysqlid...."><br>
</form>

// so then on the landing page:

$MAXIDS_PER_PAGE=100;

for ($i=0; $i<$MAXIDS_PER_PAGE; $i++) {

  $res = "delete_mysqlid".$i;

  if ($_POST["$res"] == 'on') {
      // delete item number $i
  }

}
Hope this makes sense
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yeah, use POST. GET shouldn't be used to perform an action. Say a search engine visits your delete URL and all your content is gone? That's no good.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I learned that the hard way once :oops:

I couldn't understand for the life of me why my records were disappearing!

If you read up on REST design and you'll get a fairly full dose of best practices when dealing with resource mapping.
Post Reply