6 Replies - 1616 Views - Last Post: 17 March 2013 - 02:28 PM Rate Topic: -----

#1 Chrisangel29  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Pass value from PHP to HTML

Posted 17 March 2013 - 12:51 PM

Which is the correct way to pass value from php to html ? :bigsmile:


Php file



<?php
     
	 $name  = 'chris';
	 $address = 'manhatan';
	 
?>




html file


<html>
<head><title> </title></head>

<body> 
     
	 <input type = "text" name = "name"    />
	 <input type = "text" name = "address" />

</body>
</html>



This post has been edited by Chrisangel29: 17 March 2013 - 12:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pass value from PHP to HTML

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 10,703
  • Joined: 12-December 12

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 01:01 PM

<input type = "text" name = "name" value="<?php echo $name; ?>" />

is one way - I couldn't say that it is the correct way, given that it is preferable to keep the PHP and HTML separate.

Your description indicates a misunderstanding: nothing is passed "from php to html". PHP can construct HTML, or snippets of PHP can be embedded in HTML (as in my code sample).
Was This Post Helpful? 2
  • +
  • -

#3 Chrisangel29  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 01:07 PM

Thanks my english is just bad :P/>

so which is the correct way to do it?

This post has been edited by Dormilich: 17 March 2013 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 01:12 PM

View Postandrewsw, on 17 March 2013 - 09:01 PM, said:

Your description indicates a misunderstanding: nothing is passed "from php to html". PHP can construct HTML, or snippets of PHP can be embedded in HTML (as in my code sample).

that pretty much explains it.

@chris: just because your English is bad does not mean that andrewsw’s explanation is wrong. he did a proper answer to your question.

This post has been edited by Dormilich: 17 March 2013 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 Chrisangel29  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 01:32 PM

View PostDormilich, on 17 March 2013 - 01:12 PM, said:

View Postandrewsw, on 17 March 2013 - 09:01 PM, said:

Your description indicates a misunderstanding: nothing is passed "from php to html". PHP can construct HTML, or snippets of PHP can be embedded in HTML (as in my code sample).

that pretty much explains it.

@chris: just because your English is bad does not mean that andrewsw’s explanation is wrong. he did a proper answer to your question.


i clicked +1 to his post ..because his answer helped me..i dont know what are you talking about :1eye:
Was This Post Helpful? 0
  • +
  • -

#6 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 01:34 PM

You use the echo() function.
<?php

$title = "My clever title";

?>
<html>
    <h1><?php echo($title); ?></h1>
</html>


All in one page. When someone requests your page, your server will interpret the PHP and output the echoed text.
Was This Post Helpful? 1
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Pass value from PHP to HTML

Posted 17 March 2013 - 02:28 PM

One important thing to keep in mind when printing things into a HTML document is whether or not what you are printing can include unsafe, user-supplied data. (Including things like database results and other external resources.) If that is the case, you should always pass them through either the htmlentities or html_special_chars functions before printing them. This will make sure the things you are printing are shown as text, rather than being read by the browser as part of the HTML structure. (Assuming you aren't intending for that to happen.)

For example, say you have a bunch of user comments to print:
foreach ($userComments as $comment) {
    echo "<p class='user-comment'>", htmlentities($comment, ENT_QUOTES, "UTF-8"), "</p>\n";
}


Now the output will be safe from XSS attacks, and anything in the post that could be mistaken for HTML will now be shown as text.

View PostAVReidy, on 17 March 2013 - 08:34 PM, said:

You use the echo() function.

That's a common misconception. echo is in fact not a function, but a language construct. The importance here is that constructs, as opposed to functions, can (and should) be called without parenthesis.
// These are identical.
echo $stuff;
echo($stuff);



Another critical note here is that if you intend to pass more than one thing to an echo call, it must not include the parenthesis.
// Incorrect
echo($first, $second, $third);

// Correct!
echo $first, $second, $third;


Was This Post Helpful? 3
  • +
  • -

Page 1 of 1