Jump to content

using function


dan_t

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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.

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.