APD1993 Posted January 24, 2012 Share Posted January 24, 2012 Hi, I am fairly new to PHP and I was wondering whether the program here will work and that the validation in it will work (i.e. so that if a user enters a number below 1 or above 20, they are informed of their mistake and that if they do not enter anything, then they are informed and that if they enter something that is not a number, they are told). I have also downloaded WAMP and when I try to run the file on a web browser, I get an error saying "Parse error: syntax error, unexpected '=', expecting '}' in C:\wamp\www\Guessing.php on line 32" and I see no error. EDIT: The program is a guessing game, where the user has to try to guess a random number between 1 and 20F Here is the coding: <?php header("Cache-Control:no-cache"); function setnum() { global $num; $num=rand(1,20); } ?> <html><head><title>Guessing Game</title></head> <body> <?php $num= check_input($_POST['num']); $guess = check_input($_POST['guess']); $self=check_input($_SERVER['PHP_SELF']); if ($num==null) { $msg="I have thought of a number between 1 and 20"; $msg .="<h3>guess what it is... </h3>"; setnum(); } else { if (is_numeric($guess) && $guess=$num) { $msg = "CORRECT-THE NUMBER WAS $num"; $msg .= "<h3><a href=\"$self\">"; $msg .= "CLICK HERE TO TRY AGAIN</a></h3>; setnum(); } else if (is_numeric($guess) && $guess>$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is lower</h3>";} else if (is_numeric($guess) && $guess<$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is higher</h3>";} else if ((is_numeric($guess) && $guess<1) or (is_numeric($guess) && $guess>20) { $msg="You have entered a number outside of the range of the game<h3>Please enter a number between 1 and 20</h3>; } else if (empty($_POST['guess'])) { $msg="You did not enter anything into the text box that was provided<h3>Please enter a number between 1 and 20</h3>; } else { $msg="You did not enter a number into the text box that was provided<h3>Please enter a number between 1 and 20</h3>; } } echo ($msg); ?> <form action = "<?php $self ?>" method="post"> <input type = "hidden" name = "num" value = "<?php echo $num; ?>"> Guess: <input type="text" name = "guess"> <input type = "sumbit" value = "Submit"> </form> </body> </html> <?php function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Thanks in advance, Andrew Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/ Share on other sites More sharing options...
nine7ySix Posted January 24, 2012 Share Posted January 24, 2012 First off, check your quotes. You are missing double quotes on lines 28, 38, 42 and 46. You are also missing a closing bracket on line 36. On line 55, you also misspelling submit, causing it to be a textbox instead of a submit button. I'll update this once I find other errors. And finally, on line 24, you wrote if (is_numeric($guess) && $guess=$num) Watch your usage of = and == Simply change line 24 to if (is_numeric($guess) && $guess==$num) Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310621 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 First off, check your quotes. You are missing double quotes on lines 28, 38, 42 and 46. You are also missing a closing bracket on line 36. On line 55, you also misspelling submit, causing it to be a textbox instead of a submit button. I'll update this once I find other errors. And finally, on line 24, you wrote if (is_numeric($guess) && $guess=$num) Watch your usage of = and == Simply change line 24 to if (is_numeric($guess) && $guess==$num) Thanks for the help, but it's now kicking up a fuss about "Parse error: syntax error, unexpected '{' in C:\wamp\www\Guessing2.php on line 35" :-\ Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310629 Share on other sites More sharing options...
nine7ySix Posted January 24, 2012 Share Posted January 24, 2012 Whenever I apply the changes I suggested, the code works for me. This is what I have <?php header("Cache-Control:no-cache"); function setnum() { global $num; $num=rand(1,20); } ?> <html><head><title>Guessing Game</title></head> <body> <?php $num= check_input($_POST['num']); $guess = check_input($_POST['guess']); $self=check_input($_SERVER['PHP_SELF']); if ($num==null) { $msg="I have thought of a number between 1 and 20"; $msg .="<h3>guess what it is... </h3>"; setnum(); } else { if (is_numeric($guess) && $guess==$num) { $msg = "CORRECT-THE NUMBER WAS $num"; $msg .= "<h3><a href=\"$self\">"; $msg .= "CLICK HERE TO TRY AGAIN</a></h3>"; setnum(); } else if (is_numeric($guess) && $guess>$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is lower</h3>";} else if (is_numeric($guess) && $guess<$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is higher</h3>";} else if ((is_numeric($guess) && $guess<1) or (is_numeric($guess) && $guess>20)) { $msg="You have entered a number outside of the range of the game<h3>Please enter a number between 1 and 20</h3>"; } else if (empty($_POST['guess'])) { $msg="You did not enter anything into the text box that was provided<h3>Please enter a number between 1 and 20</h3>"; } else { $msg="You did not enter a number into the text box that was provided<h3>Please enter a number between 1 and 20</h3>"; } } echo ($msg); ?> <form action = "<?php $self ?>" method="post"> <input type = "hidden" name = "num" value = "<?php echo $num; ?>"> Guess: <input type="text" name = "guess"> <input type = "submit" value = "Submit"> </form> </body> </html> <?php function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Can you test to see if it works? Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310630 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 Whenever I apply the changes I suggested, the code works for me. This is what I have <?php header("Cache-Control:no-cache"); function setnum() { global $num; $num=rand(1,20); } ?> <html><head><title>Guessing Game</title></head> <body> <?php $num= check_input($_POST['num']); $guess = check_input($_POST['guess']); $self=check_input($_SERVER['PHP_SELF']); if ($num==null) { $msg="I have thought of a number between 1 and 20"; $msg .="<h3>guess what it is... </h3>"; setnum(); } else { if (is_numeric($guess) && $guess==$num) { $msg = "CORRECT-THE NUMBER WAS $num"; $msg .= "<h3><a href=\"$self\">"; $msg .= "CLICK HERE TO TRY AGAIN</a></h3>"; setnum(); } else if (is_numeric($guess) && $guess>$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is lower</h3>";} else if (is_numeric($guess) && $guess<$num && $guess>0 && $guess<21) {$msg="You guessed $guess<h3>My number is higher</h3>";} else if ((is_numeric($guess) && $guess<1) or (is_numeric($guess) && $guess>20)) { $msg="You have entered a number outside of the range of the game<h3>Please enter a number between 1 and 20</h3>"; } else if (empty($_POST['guess'])) { $msg="You did not enter anything into the text box that was provided<h3>Please enter a number between 1 and 20</h3>"; } else { $msg="You did not enter a number into the text box that was provided<h3>Please enter a number between 1 and 20</h3>"; } } echo ($msg); ?> <form action = "<?php $self ?>" method="post"> <input type = "hidden" name = "num" value = "<?php echo $num; ?>"> Guess: <input type="text" name = "guess"> <input type = "submit" value = "Submit"> </form> </body> </html> <?php function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Can you test to see if it works? Tested it and apart from a couple of call stack tables appearing (I assume that they are a feature of WAMP, since when I use gPHPEdit on Ubuntu at college, I do not get anything like that), it seems to work and the validation works in the form of messages appearing alerting the user of any errors. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310633 Share on other sites More sharing options...
nine7ySix Posted January 24, 2012 Share Posted January 24, 2012 Glad to help Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310635 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 "Call stack tables"? You mean error messages? Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310636 Share on other sites More sharing options...
litebearer Posted January 24, 2012 Share Posted January 24, 2012 Pika - more like TV tables that nest under each other. Useful for holding pain meds and scotch while debugging my bad code Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310637 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 Me again with another "Parse error", this time it is "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\Accident.php on line 7" but with me being an extreme beginner at this language, I do not see any errors with my coding. The idea of this script is to allow for customers to enter in details on an accident report form and if they leave any fields blank, they are alerted as well as if they put in an unsuitable value for their age or number of weeks since the accident. Here is the coding: <?php header("Cache-Control:no-cache);?> <html><head><title>Car Accident Report</title></head> <body> <?php $firstname = check_input($_POST['firstname']); $surname=check_input($_POST['surname']); $age=check_input($_POST['age']); $weeks = check_input($_POST['weeks']); $self=$_SERVER['PHP_SELF']; if (empty($_POST['firstname']) or empty($_POST['surname']) or empty($_POST['age']) or empty($_POST['weeks'])) $msg="You have missed out one or more fields that you need to input data into"; $msg .=<h3><a href = \"$self\">"; $msg .="Click here to go back to the Accident Report Form to fill in the fields</a></h3>"; } else if ($age<18) { $msg = "You cannot submit an Accident Report if you are under 18 years of age"; $msg .="<h3><a href = \"$self\">"; $msg .="Click here to go back to the Accident Report Form to change your details if necessary</a></h3>"; else if ($weeks<0) { $msg = "You cannot enter a negative number for the number of weeks since the accident"; $msg .="<h3><a href=\"$self\">"; $msg .="Click here to go back to the Accident Report Form to change your details if necessary</a></h3>"; } else if ($weeks>0 && $weeks<1) { $msg = "You cannot make a report if the accident was a week or less ago"; $ .msg="<h3><a href = \"$self\">"; $ .msg="Click here to go back to the Accident Report Form to change your details if necessary"; } else { $msg = "First Name: $firstname"; $msg .= "Surname: $surname"; $msg .= "Age: $age"; $msg .= "Number of weeks since accident: $weeks"; $msg .= "Your report has successfully been sent to be viewed by the staff"; $msg .="<a href=\"$self\">"; $msg .="Click here to go back to the Accident Report Form if you want to submit another Accident Report</a> } echo ($msg); ?> <form action = "<php $self ?>" method = "post"> <b>First Name:</br><br> <input type = "text size = 45 name = "firstname"><br> <b>Surname:</br><br> <input type = "text size = 45 name = "surname"><br> <b>Age:</br><br> <input type = "text size = 45 name = "age"><br> <b>Weeks since accident:</br><br> <input type = "text size = 45 name = "weeks"><br> <input type = "submit" value = "Submit this report"> <input type = "reset" value = "Clear"> </form> </body> </html> <?php function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars ($data); return $data; } ?> I'll probably kick myself for this, but I may need some assistance Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310649 Share on other sites More sharing options...
Psycho Posted January 24, 2012 Share Posted January 24, 2012 I don't know what editor you are using, but you need to find one that colorizes the code such as this forum does when using [ php ] tags. You will then get accustomed to seeing certain elements in certain colors. Just "looking" at the code you posted I was able to easily spot at least one error. Looking at the last echo() statement the command is in red fond whereas all the other functions are in green. That is because you forgot to close the last text string with double quotes. Also, you run the POST vars through a function to parse them and assign to new variables, but then you use the original POST vars when doing the validation. Lastly, the mistakes you are making are simple syntax errors. You obviously know that a string starting with a quote mark needs to have the ending quote mark because you did it correctly many other times. So, stop excusing yourself for being a beginner. Programming is a VERY unforgiving process. You need to dot ALL your I's and cross ALL your T's. Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310652 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 I don't know what editor you are using, but you need to find one that colorizes the code such as this forum does when using [ php ] tags. You will then get accustomed to seeing certain elements in certain colors. Just "looking" at the code you posted I was able to easily spot at least one error. Looking at the last echo() statement the command is in red fond whereas all the other functions are in green. That is because you forgot to close the last text string with double quotes. Also, you run the POST vars through a function to parse them and assign to new variables, but then you use the original POST vars when doing the validation. Lastly, the mistakes you are making are simple syntax errors. You obviously know that a string starting with a quote mark needs to have the ending quote mark because you did it correctly many other times. So, stop excusing yourself for being a beginner. Programming is a VERY unforgiving process. You need to dot ALL your I's and cross ALL your T's. I changed the echo line of code, but there are still errors with line 7 of the code apparently, even though it looks fine to me :/ Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310671 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 There's a problem in the very first line, in the header() call. Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310674 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 There's a problem in the very first line, in the header() call. Thanks for that I think I'm getting closer, but there's a problem with my code at the moment. If I enter information into all of the fields, I would like the information to then be displayed, but only the "Welcome" message seems to appear and I cannot view the text that should appear based on either correct or incorrect information. I think it may be due to the "hidden" aspect of my form at the end of my form coding. My coding as of now is as follows: <?php header("Cache-Control:no-cache");?> <html><head><title>Car Accident Report</title></head> <body> <?php $firstname = check_input($_POST['firstname']); $surname=check_input($_POST['surname']); $age=check_input($_POST['age']); $weeks = check_input($_POST['weeks']); $self=$_SERVER['PHP_SELF']; if ($firstname==null && $surname==null && $age==null && $weeks==null) { $msg="Welcome to the Car Accident Report Form<br>"; $msg .="Please enter in the relevant details in the text boxes"; } else { if ($age<18) { $msg = "You cannot submit an Accident Report if you are under 18 years of age"; $msg .="<h3><a href = \"$self\">"; $msg .="Click here to go back to the Accident Report Form to change your details if necessary</a></h3>"; } if (empty($_POST['firstname']) or empty($_POST['surname']) or empty($_POST['age']) or empty($_POST['weeks'])) { $msg="You have missed out one or more fields that you need to input data into"; $msg .="<h3><a href = \"$self\">"; $msg .="Click here to go back to the Accident Report Form to fill in the fields</a></h3>"; } else if ($weeks<0) { $msg = "You cannot enter a negative number for the number of weeks since the accident"; $msg .="<h3><a href=\"$self\">"; $msg .="Click here to go back to the Accident Report Form to change your details if necessary</a></h3>"; } else if ($weeks>0 && $weeks<1) { $msg = "You cannot make a report if the accident was a week or less ago"; $msg .="<h3><a href = \"$self\">"; $msg .="Click here to go back to the Accident Report Form to change your details if necessary"; } else { $msg = "First Name: $firstname"; $msg .= "Surname: $surname"; $msg .= "Age: $age"; $msg .= "Number of weeks since accident: $weeks"; $msg .= "Your report has successfully been sent to be viewed by the staff"; $msg .="<a href=\"$self\">"; $msg .="Click here to go back to the Accident Report Form if you want to submit another Accident Report</a>"; } } echo ($msg); ?> <form action = "<?php $self ?>" method = "post"> <b>First Name:</br><br> <input type = "text size = 45 name = "firstname"><br> <b>Surname:</br><br> <input type = "text size = 45 name = "surname"><br> <b>Age:</br><br> <input type = "text size = 45 name = "age"><br> <b>Weeks since accident:</br><br> <input type = "text size = 45 name = "weeks"><br> <input type = "submit" value = "Submit this report"> <input type = "reset" value = "Clear"> <input type="hidden" name= "form" value = "<?php $msg; ?>"> </form> </body> </html> <?php function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars ($data); return $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310685 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 Can any help be provided? Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310705 Share on other sites More sharing options...
APD1993 Posted January 24, 2012 Author Share Posted January 24, 2012 I decided to go through this in another way that I used before which was to have a PHP script linked to an HTML page. I would like it so that when the PHP script is run, the user can go back to the HTML page, however, I am having some difficulty with being able to show a link on a PHP script that will go back to an HTML page. Here is my code: <html><head><title>Maths Program</title></head> <body> //In this block of code I am creating a PHP script with some validation so as to show the results of performing mathematical operations on two numbers entered by the user //Validation is also used so that two numbers are entered by the user, one in each text box <?php $number1= $_POST ['number1']; $number2=$_POST ['number2']; if (is_numeric($number1)&&is_numeric($number2)) { $addtotal=$number1+$number2; $subtracttotal=$number1-$number2; $multiplytotal=$number1*$number2; $dividetotal=$number1/$number2; echo ("Your numbers were $number1 and $number2<br>"); echo ("$number1 + $number2= $addtotal<br>"); echo ("$number1 - $number2= $subtracttotal<br>"); echo ("$number1 * $number2= $multiplytotal<br>"); echo ("$number1 / $number2= $dividetotal<br>"); echo ("Click here to go back: <a href="localhost/Sum.htm">Want to choose more numbers?</a>); } else if (empty($number1) or empty($number2)) {echo ("Please enter two numbers and try again");} ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310715 Share on other sites More sharing options...
nine7ySix Posted January 25, 2012 Share Posted January 25, 2012 Look on line 19 echo ("Click here to go back: <a href="localhost/Sum.htm">Want to choose more numbers?</a>); You need a closing quotation. Also, you can't have double quotes within double quotes, that is a big no no. It should be echo ("Click here to go back: <a href='localhost/Sum.htm'>Want to choose more numbers?</a>"); or echo ('Click here to go back: <a href="localhost/Sum.htm">Want to choose more numbers?</a>'); Try using a PHP editor so you can see your mistakes easier. I hear NotePad++ is good for Windows and Textmate is good for Mac. Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310840 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2012 Share Posted January 25, 2012 Or more accurately, if you need to use quotes within the same type of quotes in a string, they must be escaped with a backslash. $string = "This is a double quoted \"string\" with double quotes in it."; $string1 = 'This is a single quoted\'string\' with single quotes in it''; You can use the opposite type of quote within a string without escaping them. If you want to use variables in a quoted string, you need to use double quotes to enclose the string. Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310845 Share on other sites More sharing options...
nine7ySix Posted January 25, 2012 Share Posted January 25, 2012 Or more accurately, if you need to use quotes within the same type of quotes in a string, they must be escaped with a backslash. $string = "This is a double quoted \"string\" with double quotes in it."; $string1 = 'This is a single quoted\'string\' with single quotes in it''; You can use the opposite type of quote within a string without escaping them. If you want to use variables in a quoted string, you need to use double quotes to enclose the string. That being said, you have two single quotes at the end of $string1, either accidental or you forgot to escape Quote Link to comment https://forums.phpfreaks.com/topic/255670-validation-help-and-parse-error/#findComment-1310860 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.