dan_t Posted April 24, 2010 Share Posted April 24, 2010 Hi was just messing around, and can't figure out how to use this on more than one variable. $string = $_POST['user_name']; $string = $_POST['user_id']; function Clean_input($string) { $string = $_POST['user_id']; $string = strip_tags($string); $string = stripslashes($string); $string = htmlspecialchars($string); $string = trim($string); return$string; } echo <<<EOF <form action="my_first_function.php" name="form" method="POST"> <input type="text" name="user_name"> <input type="text" name="user_id"> <input type="submit" name="submit" value="send"> </form> EOF; echo Clean_input($string); I can get it to print out one, but not two without it saying I can't redeclare the function. I got it to print them both out once, but thatcounldn't have been the correct way. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 24, 2010 Share Posted April 24, 2010 $cleaned = array_map('Clean_input', $_POST)? Quote Link to comment Share on other sites More sharing options...
dan_t Posted April 24, 2010 Author Share Posted April 24, 2010 I wasn't using an array. Is that necessary in this case, being so basic? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 24, 2010 Share Posted April 24, 2010 It looks like you're trying to send parts of the $_POST array through the function, yes? Then you'd just access the indices via $cleaned['user_name'] and $cleaned['user_id'], or whatever other indices from the post array you were using. If you don't need a certain index run through and cleaned, you can still access it through $_POST['index'] Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 25, 2010 Share Posted April 25, 2010 your overwitting your $string variable so it only ends up with the last thing you assign it. $string1 = $_POST['user_name']; $string2 = $_POST['user_id']; echo clean($string1); echo clean($string2); also, the code you posted wouldnt cause a function redeclared error Quote Link to comment Share on other sites More sharing options...
dan_t Posted April 25, 2010 Author Share Posted April 25, 2010 yeah your right, I got the redeclare when i tried using the full function, but only changing $string to $string1. Quote Link to comment Share on other sites More sharing options...
dan_t Posted April 25, 2010 Author Share Posted April 25, 2010 $cleaned = array_map('Clean_input', $_POST)? So I'm not exactly sure how this works, but is it the one size fits all? I know I tried several different ways but could only get one part to go in due to the 'user_name' or where it said $string = $_POST['user_name']; I can't figure how to make 'user_name' generic. or a useable variable. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 Using it with the syntax of $new_array = array_map('[function-name]', [$array-name]), it will run every index in the array through the specified function and return a new array index as $new_array['new_index']. If the $_POST array is $_POST['name'], $_POST['id'] and $_POST['email'], $new_array = array_map('Clean_input', $POST) will return $new_array['name'], $new_array['id'] and $new_array['email'] after sending them to the specified function. So it would be the same as calling the function for each index of the array, but it all gets done at once and builds you a new array of values. I hope I'm not overcomplicating the explanation . . . Also have a look at the PHP manual page: http://php.net/manual/en/function.array-map.php Quote Link to comment Share on other sites More sharing options...
Graxeon Posted April 25, 2010 Share Posted April 25, 2010 I've seen a similar thing to this so I hope it helps: $xml = ''; $xml .= 'hello'; $xml .= 'world'; print $xml though idk exactly what your code means Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 Just noticed the second part of that: "I can't figure how to make 'user_name' generic. or a useable variable." and I'm not sure what you mean. Are you trying to use $POST['user_name'] as the variable $user_name ? If so, You need to assign it explicitly via $user_name = $_POST['user_name']; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 I've seen a similar thing to this so I hope it helps: $xml = ''; $xml .= 'hello'; $xml .= 'world'; print $xml though idk exactly what your code means That is simply string concatenation. Not really related to this issue. Quote Link to comment Share on other sites More sharing options...
dan_t Posted April 25, 2010 Author Share Posted April 25, 2010 Just noticed the second part of that: "I can't figure how to make 'user_name' generic. or a useable variable." and I'm not sure what you mean. Are you trying to use $POST['user_name'] as the variable $user_name ? If so, You need to assign it explicitly via $user_name = $_POST['user_name']; Well the Clean_code function, I'm trying to make a function that can be used on every input item off the form. I am currently writing this code individually for every entry on the form. Like $firstName = $_POST['firstName']; $firstName = strip_tags($firstName); $firstName = stripslashes($firstName); $firstName = htmlspecialchars($firstName); $firstName = trim($firstName); $lastName = $_POST['lastName']; $lastName = strip_tags($lastName); $lastName = stripslashes($lastName); $lastName = htmlspecialchars($lastName); $lastName = trim($lastName); $password = $_POST['password']; $password1 = $_POST['password1']; $password = strip_tags($password); $password = stripslashes($password); $password = htmlspecialchars($password); $password = trim($password); I would like one function to cover each one , or is the the correct way. It seems long winded. I'm really only focusing on the function itself. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.