当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Get Tag As Value For Imput Type

问题描述:

I am needing a GET value to be placed display in a forum option, however I do not know how to do this, as the get value can not have quotes in it. The xxx is where I want to display the get value.

<input type="text" name="test" value="xxx">

网友答案:

This is really basic stuff. Should not be any problems to find by Google.

<input type="text" name="test" value="<?php echo $_GET['your_parameter']; ?>" />
网友答案:

You should escape the GET parameter before echoing it, unlike the other answer. This will prevent someone from injecting extraneous tags into your code.

<?php 

$parameter = (isset($_GET["your_parameter"])) ? htmlspecialchars($_GET["your_parameter"]) : '';

?>

<input type="text" name="test" value="<?= $parameter ?>" />

If you have short tags disabled, you'll need to use <?php echo in place of <?=. Many (most?) PHP developers will argue that you shouldn't use short tags at all. I personally never use them because I don't write my templates in PHP.

网友答案:

As Jimmie Johansson mentioned before this is really basic stuff and easy to search for.

Hi <?php echo htmlspecialchars($_POST['name']); ?>. 
You are <?php echo (int)$_POST['age']; ?> years old.

Source: http://www.php.net/manual/en/tutorial.forms.php

This is an example which i found in about 10 seconds. But enough of the flame lets get to business.

If you transmit data via a FORM to php there are to ways to transmit the data. The first one ist via GET the other way is via POST.

If you choose GET your information will be stored as URL-Parameters

An associative array of variables passed to the current script via the URL parameters.

Source: http://www.php.net/manual/en/reserved.variables.get.php

Post instead just sends the data to the PHP-Script so you can use this data like GET but you do not have any URL-Parameters here.

An associative array of variables passed to the current script via the HTTP POST method.

Source: http://www.php.net/manual/en/reserved.variables.post.php

To choose one of this two Methods you have to edit your HTML-Form

Now that we transmitted the data either with GET or POST we can access them in PHP via the global variables

$_GET

and

$_POST

They are Arrays and you can access specific data with a key which represents the name of the input field or other form constructs. In your example the input fields name is "test" so we would access it like this

<?php $_GET["test"]; ?>

You can use

<?php ... ?>

Wherever you want in your HTML-Code as long it's an PHP File. So if you want to output something via PHP just use the and put you statements between them.

<input type="text" name="test" value="<?php echo $_GET['your_parameter']; ?>" />

Documentation

Arrays:

  1. http://php.net/manual/de/language.types.array.php

Forms:

  1. http://www.php.net/manual/en/tutorial.forms.php
  2. http://www.w3schools.com/php/php_forms.asp

PHP and HTML:

  1. http://www.php.net/manual/en/faq.html.php

POST and GET:

  1. http://php.net/manual/de/reserved.variables.post.php
  2. http://php.net/manual/de/reserved.variables.get.php

Further Reading

Further reading

  • http://en.wikipedia.org/wiki/Separation_of_presentation_and_content
  • http://wp.tutsplus.com/tutorials/creative-coding/improving-your-work-flow-separate-your-mark-up-from-your-logic/
  • http://www.w3schools.com/tags/ref_httpmethods.asp

And the important things at the end. For security you should check what was entered into the input field

http://www.w3schools.com/php/php_form_validation.asp

Sorry for my englisch! If you have problems to follow my answer feel free to ask me. Check the links!

分享给朋友:
您可能感兴趣的文章:
随机阅读: