Page 1 of 1

problem in this phpcode

Posted: Tue Jul 07, 2009 12:52 pm
by Naeem
I have a problem in this php code i can't find where i am wrong here is my code i am learning php and it's a simple form application.
Here is a html form:

Code: Select all

<html>
<head><title>Job Application</title></head>
<style type="text/css">
body {
    border: 2% solid #dddddd;
    margin: 4%;
    padding: 4%;    
}
.button {
    border:1px inset #dddddd;
}
</style>
<body>
<center><form method="post" action="application.php">
<table border="1" cellpadding="2" cellspacing="0" width="75%" style="border-collapse:collapse;border-bottom:1px solid #dddddd;border-left:2px solid #ccc;border-right:2px solid #ccc;border-top:1px solid #dddddd;">
<tr><td><b>Name:</b></td><td><input type="text" name="name"/></td></tr>
<tr><td><b>Phone:</b></td><td><input type="text" name="phone"/></td></tr>
<tr><td><b>Email:</b></td><td><input type="text" name="email"/></td></tr>
<tr><td><b>Address:</b></td><td><textarea name="address"/></textarea></td></tr>
<tr><td><b>Gender:</b></td><td><b>Male:</b><input type="radio" name="gender" value="Male"/>&nbsp;<b>Female:</b><input type="radio" name="gender" value="female"/></td></tr>
<tr><td><b>City:</b></td><td><select name="city"><option>Karachi</option>
<option>Islamabad</option><option>Lahore</option></select></td></tr>
<tr><td><b>Age:</b></td><td><input type="text" name="age"/></td></tr>
<tr><td><b>Skills:</b></td><td><b>PHP:</b><input type="checkbox" name="skills[]" value="php"/><br /><b>MySQL:</b><input type="checkbox" name="skills[]" value="mysql"/><br /><b>HTML:</b><input type="checkbox" name="skills[]" value="html"/><br /><b>AJAX:</b><input type="checkbox" name="skills[]" value="ajax"/><br /><b>Flash:</b><input type="checkbox" name="skills[]" value="flash"/></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Submit" class="button"/>&nbsp;<input type="reset" value="Reset" class="button"/></td></tr>
</table>
</form></center>
</body>
</html>
And here is a PHPCODE:

Code: Select all

<html>
<head><title>Process Application</title></head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
//Function for clean string
function cleanstr($clstr){
    $cstr = trim($clstr);
    $cstr = addslashes($clstr);
    $cstr = htmlspecialchars($clstr);
    return $cstr;
}
function emptyfields($empty){
    if(!is_array($empty)){
        echo "The argument must be in array";
        return false;
    }
    foreach($empty as $required => $values){
        $values = cleanstr($values);
        if(empty($values)){
            echo "<b>" . ucwords($required) . "</b> must not be empty";
            return false;
        }
    }
    return true;
}
//calling function to check any empty field
if(!emptyfields($_POST)){
    exit();
}
$name = cleanstr($_POST['name']);
$phone = cleanstr($_POST['phone']);
$email = cleanstr($_POST['email']);
$address = cleanstr($_POST['address']);
$gender = $_POST['gender'];
$city = $_POST['city'];
$age = (int)cleanstr($_POST['age']);
$skills = $_POST['skills'];
echo "Your name is:". $name;
echo "Your Phone no is:". $phone;
echo "Your email is:". $email;
echo "Your address is:". $address;
echo "Your gender is:". $gender;
echo "Your city is:". $city;
echo "Your age is:". $age;
echo "Your skills: ";
foreach($skills as $value){
    echo ($value).",";
}
 
?>
</body>
</html>

Re: problem in this phpcode

Posted: Tue Jul 07, 2009 1:28 pm
by requinix
Naeem wrote:I have a problem in this php code i can't find where i am wrong
And I can't find where you tell us what the problem is.

Re: problem in this phpcode

Posted: Wed Jul 08, 2009 2:25 am
by Naeem
tasairis wrote:
Naeem wrote:I have a problem in this php code i can't find where i am wrong
And I can't find where you tell us what the problem is.
Oh really sorry mate: :cry:
This is a problem:

Code: Select all

Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\wamp\www\application.php on line 10
Skills must not be empty

Re: problem in this phpcode

Posted: Wed Jul 08, 2009 2:34 am
by SvanteH

Code: Select all

function cleanstr($clstr){
    $cstr = trim($clstr);
    $cstr = addslashes($clstr);
    $cstr = htmlspecialchars($clstr);
    return $cstr;
}
The problem for starters on this one is that you keep giving $clstr as input variable on each function. Why not

Code: Select all

    $cstr = trim($clstr);
    $cstr = addslashes($cstr);
    $cstr = htmlspecialchars($cstr); 
Right now you're function is equivalent to $cstr = htmlspecialchars($clstr); only.
Second problem, when you run emptyfields is that $_POST["skills"] is an array. You treat it as it were a string, thus I recommend.

Code: Select all

foreach($empty as $required => $values){
  if(!is_array($values))
  {
Or check whether it's an array and do another foreach loop to break it out

Re: problem in this phpcode

Posted: Wed Jul 08, 2009 3:01 am
by Naeem
My problem is this that i am keep giving $clstr as input variable on each function

Code: Select all

$cstr = trim($clstr);
    $cstr = addslashes($cstr);
    $cstr = htmlspecialchars($cstr
so the problem is solved but dear Svanteh i am newbie in php so please can you explain what's the problem is this.
Thanks.

Re: problem in this phpcode

Posted: Wed Jul 08, 2009 3:05 am
by SvanteH

Code: Select all

<?php
  $aString = "This is a text";
  
  $newString = $aString . " from";
  //newString = This is a text from
  
  $newString = $aString . " my"
  //newString = This is a text my
?>
 
 
 
<?php
  $aString = "This is a text";
  
  $newString = $aString . " from";
  //newString = This is a text from
  
  $newString = $newString . " my"
  //newString = This is a text from my
?>
I hope this helps, it's quite hard explaining in words something I just find naturally logical. Once you get this it'll be easy :)

Re: problem in this phpcode

Posted: Wed Jul 08, 2009 3:23 am
by Naeem
Thanks mate :)