-2
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo "Homework1_1"?></title>
</head>
<body>

<form action="" method="post">
<input name ="name" type = "text" placeholder="Type your Name">
<input name = "age" type = "text" placeholder = "Type your age">
<input type = "submit">
</form>
<?php
if(isset($_POST["name"])){
$Name =["name"];
$Age = ["age"];
if(preg_match("[0-10]", $name)){
    echo "Failed, name can not be number";
}
 if(preg_match("/[a-zA-Z + ] +/",$ar)){
    echo "Failed, age cannot have letter";
}
if((!preg_match("[0-10]", $Name))&&(!preg_match("/[a-zA-Z + ] +/",$Age))){
    echo "Welcome! Your name is $Name and you are $Age years old";
}   
}
 ?>
</body>
</html>

This is my program code. I want if I write number in $Name=["name"] or letter in $Age=["age] the website would show an error message like in the block, but when I run the code and type some values to the both boxes, the website every time shows me

Warning: preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 18

Warning: preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 21

Warning: preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 26

Warning: preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 26

Notice: Array to string conversion in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 27

Notice: Array to string conversion in C:\xampp\htdocs\php_workshop_1_citat\GrundOvningar\homework1_1_post.php on line 27

Welcome! Your name is Array, and you are Array years old

What did I do wrong? Maybe I don't need use preg_match? If that is so, what can I do?

Sourav Ghosh
  • 1,964
  • 4
  • 33
  • 43
javaprogrammer
  • 105
  • 1
  • 2
  • 11

2 Answers2

3

PHP variables are case sensitive

if(isset($_POST["name"])) {
  $name = $_POST["name"];
  $age = $_POST["age"];
  if(preg_match("[0-10]", $name)){
    echo "Failed, name can not be number";
  }
  if(preg_match("/[a-zA-Z + ] +/", $age)) {
    echo "Failed, age cannot have letter";
  }
  if((!preg_match("[0-10]", $name)) && (!preg_match("/[a-zA-Z + ] +/",$age))) {
    echo "Welcome! Your name is $Name and you are $Age years old";
  }   
}
Jaime
  • 1,402
  • 7
  • 15
1

Your code is wrong

$name =["name"];
$age = ["age"];

should be

$name = $_POST["name"];
$age = $_POST["age"];

Also, your preg_match should be:

For numbers:

preg_match("/[0-9]/", $name)

For letters:

preg_match("/[a-zA-Z]/", $age)
leoap
  • 1,684
  • 3
  • 24
  • 32
  • Thanks! Now my program does not show any error but when I type numbers in name box, for example 3, the website shows me that my name is 3. The same with age but that is not what I want. They must show error! Do you know how to do with that? – javaprogrammer Nov 08 '16 at 18:59
  • your preg_match is wrong too, to check numbers use [0-9], and to check letters use [a-zA-Z] – leoap Nov 08 '16 at 19:02
  • Which preg_match do you mean? That is my first time I have used preg_match in my code so I don't know what do you mean exactly – javaprogrammer Nov 08 '16 at 19:06
  • Thanks ! I did not see that you edited your answer. I will test that – javaprogrammer Nov 08 '16 at 19:07
  • Thank you thank you thank you very much! I got the right result which I wanted! – javaprogrammer Nov 08 '16 at 19:09