Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Performing a function on all post data from a form

Featured Replies

If I wanted to run a particular function or other bit of code, such as stripslashes used here, on all data coming in from a form before I proceed to check individual variables in the usual way, would this be a good way of doing it?

function get($key, $source=$_POST) (
$item = $source[$key];
return stripslashes($item);
}

Is this code correct? Safe? Reliable? Or not.

I have not had a chance to test it yet.

If I wanted to run a particular function or other bit of code, such as stripslashes used here, on all data coming in from a form before I proceed to check individual variables in the usual way, would this be a good way of doing it?

function get($key, $source=$_POST) (
$item = $source[$key];
return stripslashes($item);
}

Is this code correct? Safe? Reliable? Or not.

I have not had a chance to test it yet.

 

 

Do something like this

 

 

function protect($string){
$string = strip_tags($string);
$string = stripslashes($string);
return $string;
}

 

The use it on a post variable like this

 

 


$name = (isset($_POST['name'])) ? protect($_POST['name']) : FALSE;

  • Author

Thanks for the reply, but I'm not going to run a function across 15 post variables separately. That's exactly the kind of code bloat :blink: that I want to avoid if at all possible. That's the point of my question, basically: anything that streamlines the process is welcome.

while(list($key,$value) = each($_POST)) {
//do what ever you want here for example
       $$key = stripslashes(strip_tags(trim($value)));
}

 

This way loops through all posted vaiables.

 

Thanks

Nathan

 

Attain Design

Edited by nathan@attaindesign.co.uk

Thanks for the reply, but I'm not going to run a function across 15 post variables separately. That's exactly the kind of code bloat :blink: that I want to avoid if at all possible. That's the point of my question, basically: anything that streamlines the process is welcome.

 

As far as i know it thats the only way to do it is put the function around each variable i could be wrong though i personally don't use anyother way and its not really code bloat its one function you just put around the variable you wanna protect when you create that variable way easier then doing it like this

$name = stripslashes(strip_tags(trim($_POST['name'])));

  • Author

@ Nathan

That looks good. Thanks. I'll try it when I can.

 

@ webdesigner93

Well, we can agree to differ about what constitutes bloat :) but I definitely don't want to write out 15 isset statements per your example if I can loop through all data in one function instead and avoid too much repetition. But thanks for your input anyway: it is always appreciated.

@ Nathan

That looks good. Thanks. I'll try it when I can.

 

@ webdesigner93

Well, we can agree to differ about what constitutes bloat :) but I definitely don't want to write out 15 isset statements per your example if I can loop through all data in one function instead and avoid too much repetition. But thanks for your input anyway: it is always appreciated.

 

Well the isset for the variables prevents the unidentified index error otherwise your just going to be filling the error log with useless errors :pardon:

  • Author

Hmmmm.... OK. Tell me a bit more about this index error!

  • Author

@ Jay

How would I get all the post variables into an array? I haven't done that before.

  • Author

Oh. Did not know that... :blush1: I hate PHP!

  • Author

@ Rallport

 

Please elaborate on what your last comment is supposed to mean.

  • Author

Yes, it did come across as a bit offensive.

 

I didn't just jump into PHP without first reading books, website tutorials, and parts of the manual. But what immediately makes perfect sense to some will be a difficult concept for others. My knowledge of it has been slowly getting there over the past year, on and off when I have time for it. My specialisms are IA and UX rather than server-side.

 

No worries, though.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.