Jump to content

Help with filter_has_var, trying to combine html and php file into one.


Go to solution Solved by requinix,

Recommended Posts

<!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

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 

  • Solution

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 = on
Then restart the web server. With those set you would have seen the error message from PHP about an "unexpected $end". Edited by requinix
<!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 .. 

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 by rwhite35

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

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

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.