• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP/Html question

l3iohazard

Limp Gawd
Joined
Dec 22, 2010
Messages
278
First of all, Im still new to php and html for the most part. My question is this, I want to go above what my assignment calls for and create a form that the user can enter their name, city, and state. Originally the assignment called for just entering my name in the variable but seemed to easy. How would I go about collecting the data the user entered into the form and adding it to the php variable? This is what I have so far. Am I even heading in the right direction?


Code:
<?PHP
echo "<form action=$PHP_SELF method=POST>
Name: <input type="text" name="firstname" />
City: <input type="text" name="city" />
State: <input type="text" name="state" />
<input type=submit value=Submit>
</form>";
?>
 
Very close, the biggest problem being your quotation marks. PHP will think the end of the string is your second set of quotations (which happen in your Name: line for "text").

You either need to switch up and use one type of quote for PHP and another type quote for HTML:
Code:
echo 'This is my quote: "Hello World!" '; 
echo "This is my quote: 'Hello World!' ";

Or escape your quotes inside of the php string:
Code:
echo "This is my quote: \"Hello World!\" ";

http://stackoverflow.com/questions/...ay-to-write-string-literals-in-php-without-or

To use a PHP variable inside of a string, you should either append it to the string:
Code:
echo "This is my variable: " . $PHP_VAR . " and it is good";
or, if you want to include it directly inside of your PHP strings, you can encase it with braces to remove any inambiguity (otherwise it acts differently depending on which type of quote you use):
Code:
echo "This is my variable: {$PHP_VAR} and it is good";

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

Another thing I find useful when doing long blocks of HTML output like that are to put \n at the end of each line, that way when you View Source inside of a web browser, it looks sane:

Code:
echo "<form action={$PHP_SELF} method=POST> \n
Name: <input type=\"text\" name=\"firstname\" /> \n
City: <input type=\"text\" name=\"city\" /> \n
State: <input type=\"tex\t" name=\"state\" /> \n
<input type=\"submit\" value=\"Submit\"> \n
</form> \n";

Otherwise, your HTML block will show up in the source as one giant long string. It's going to do that in HTML anyway unless you format it with some DIV's, BR's, P's or inside a TABLE.
 
<form action="index.php" method="post">
<input type="text" name="userName" />
<input type="password" name="password" />
<input type="submit" />
</form>

this is assuming that the page is index.php, you can change as required.

Then in php use print_r($_REQUEST); to see whats coming in.

And to assign the fields use something like:

$userName = $_POST['userName'];
$pasword = $_POST['password'];
 
Now, to get the data back out of the form:

You need to process the data if your web hit came with POST data (since you are submitting with a POST method).

PHP stores post data in a global variable called $_POST (or $_REQUEST, which holds POST, GET and COOKIE data). That variable is just a huge array that has elements that are your form input names, and the values are the user-input values.

Something along the lines of
Code:
if( $_POST['firstname'] != '')
{
  $myname = $_POST['firstname'];
  $mycity = $_POST['city'];
  etc...
  echo "Thank you {$firstname} for your input!";
}
else
{
  echo "<form> my big form...";
}
 
Back
Top