webdevdea Posted June 10, 2013 Share Posted June 10, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php if (filter_has_var(INPUT_POST, "bName") && filter_has_var(INPUT_POST, "gName") && filter_has_var(INPUT_POST, "bPart") && filter_has_var(INPUT_POST, "device")) { $bName = filter_input(INPUT_POST, "bName"); $gName = filter_input(INPUT_POST, "gName"); $bPart = filter_input(INPUT_POST, "bPart"); $device = filter_input(INPUT_POST, "device"); print <<< HERE <h2>$bName and $gName<br />Went up the hill<br /></h2> To Fetch a $device of water<br /> $bName fell down and broke his $bPart <br /> And $gName came tumbling after. </h2> \n HERE; } else { //create form for the input print <<< HERE <h3>Please fill in the blanks below, and I will tell you a story</h3> <form method = "post" action = "" > <fieldset> <label>Boys Name:</label> <input type = "text" name = "bName" /> <label>Girls Name</label> <input type = "text" name = "gName" /> <label>body part</label> <input type = "text" name = "bPart" /> <label>water holding device </label> <select name = "device"> <option value = "bucket">bucket</option> <option value = "cooler">cooler</option> <option value ="glass">glass</option> <option value = "sip">sip</option> </select> <input type="submit" value="submit"/> Tell me the story </fieldset> </form> HERE; } ?> //end if </body> </html> Above is my code, I cannot get it to run correctly, I am a newbie trying to work through a book before next semester so that I will not be so confused when the real stuff starts.. Please if you could explain where I have things wrong. Thank you webDevdea Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/ Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 And what is "run correctly" supposed to mean? Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435235 Share on other sites More sharing options...
webdevdea Posted June 11, 2013 Author Share Posted June 11, 2013 its supposed to generate a form for the user to fill out and when the user hits the select button the code generates a version of jack and jill with the words that the user put into the form. In an earlier chapter of the book I did this task with two files, this challenge was to combine the two files into one working php file.. please and thank you Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435245 Share on other sites More sharing options...
Solution requinix Posted June 11, 2013 Solution Share Posted June 11, 2013 (edited) Besides the gratuitous use of heredocs the only problem I see is the extra spaces at the end of them. print <<< HERE <h2>$bName and $gName<br />Went up the hill<br /></h2> To Fetch a $device of water<br /> $bName fell down and broke his $bPart <br /> And $gName came tumbling after. </h2> \n HERE; print <<< HERE <h3>Please fill in the blanks below, and I will tell you a story</h3> ... </form> HERE;When ending heredocs the "HERE" (or whatever) must be the absolute only thing on the line except a possible semicolon. No leading spaces, no trailing spaces, and no indentation. [edit] Find your php.ini and make sure you have error_reporting = -1 display_errors = onThen restart the web server. With those set you would have seen the error message from PHP about an "unexpected $end". Edited June 11, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435252 Share on other sites More sharing options...
webdevdea Posted June 11, 2013 Author Share Posted June 11, 2013 I was getting the unexpected end, but since I am new to programming languages, I was not sure exactly what that ment. Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435302 Share on other sites More sharing options...
webdevdea Posted June 11, 2013 Author Share Posted June 11, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <?php if (filter_has_var(INPUT_POST, "userName") && filter_has_var(INPUT_POST, "bName")){ // the form exists, so work with it $userName = filter_input(INPUT_POST, "userName") && $bName = filter_input(INPUT_POST, "bName"); print "<h2>Hi there, $userName $bName </h2> \n"; } else { //there's no input. Create the form print <<< HERE <form action = "" method = "post"> <fieldset> <label>Please enter your name</label> <input type = "text" name = "userName" /> <label>Please enter your name</label> <input type = "text" name = "bName" /> <button type = "submit"> submit </button> </fieldset> </form> HERE; } // end 'value exists' if ?> </body> </html> The above code produces a number 1 when i hit the submit button if text is entered into the first field of the form, Im sure its something small that is making this happen, if you could point it out to me please. Thank you .. Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435303 Share on other sites More sharing options...
boompa Posted June 11, 2013 Share Posted June 11, 2013 Why are you using && here? $userName = filter_input(INPUT_POST, "userName") && $bName = filter_input(INPUT_POST, "bName"); Just put those assignments on different lines. Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435305 Share on other sites More sharing options...
rwhite35 Posted June 11, 2013 Share Posted June 11, 2013 (edited) your trying to concatinate two variables but using a logic operator. Changes this: $userName = filter_input(INPUT_POST, "userName") && $bName = filter_input(INPUT_POST, "bName"); To this: $userName = filter_input(INPUT_POST, "userName").",".$bName = filter_input(INPUT_POST, "bName"); //or just $userName = filter_input(INPUT_POST, "userName").$bName = filter_input(INPUT_POST, "bName"); or as boompa suggest $userName = filter_input(INPUT_POST, "userName"); $bName = filter_input(INPUT_POST, "bName"); echo $userName."".$bName; Edited June 11, 2013 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435308 Share on other sites More sharing options...
boompa Posted June 11, 2013 Share Posted June 11, 2013 Ah, it was so early I failed to put together that the poster wanted to concatenate the two variables! Nice! Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435334 Share on other sites More sharing options...
webdevdea Posted June 11, 2013 Author Share Posted June 11, 2013 You guys are awesome, im about to put everything together and see how it works.. just so you know you guys are much more nicer than stackoverflow they are quite rude to someone that is just beginning to code like me.. again thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435336 Share on other sites More sharing options...
webdevdea Posted June 11, 2013 Author Share Posted June 11, 2013 Perfect, I am so happy so so happy, now I can try to use this to work on the bigger project that had me stumped.. thank you all so very much. my goal is to learn enough so that I can help a fellow user out as well.. Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435339 Share on other sites More sharing options...
rwhite35 Posted June 11, 2013 Share Posted June 11, 2013 No problem, we've all been there, at least I have... Quote Link to comment https://forums.phpfreaks.com/topic/279015-help-with-filter_has_var-trying-to-combine-html-and-php-file-into-one/#findComment-1435387 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.