How to print "<" and ">" stored in the variable using php? - php

<form id="preview-form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<textarea class="codemirror-textarea" name="preview-form-comment" id="preview-form-comment"><?php echo $comment;?></textarea>
<br>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/default.js"></script>
<input type="submit" onclick="savedata();" name="preview-form-submit" id="preview-form-submit" value="Submit">
</form>
When I do echo $_POST['preview-form-comment']; in the PHP script and the text in the textarea is #include<stdio.h> it only prints #include and takes the other part as a tag.

You need to escape any HTML characters that might be in your variable using htmlentities():
<?= htmlentities($comment) ?>

htmlspecialchars() would do it.
Try it with echo htmlspecialchars($_POST['preview-form-comment'])

Related

I can't transport HTML information to PHP

My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>

Extra character from textarea

I have two pages like this:
test.html
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="test2.php">
<textarea id="input" name="input" cols="100px" rows="15" wrap="soft" placeholder="put input here"></textarea>
<button type="button" onclick="getLength()">Get length </button><p id="length"> </p></div>
<br >
<input type="submit">
</form>
<script>
function getLength(){
var len=$('textarea').val().length;
$('#length').text(len);
}
</script>
</body>
</html>
And test2.php
<?php
echo strlen($_POST['input']);
?>
When I enter some input eg: "aa[pressEnter]aa", It should echo 5 at test2.php page but it echoes 6 instead.
So why it is 6 (what is extra character) ?
thanks for reading!
Return is counted as an extra character in chrome.
Actually return is 2 escape characters \r\n. In chrome it takes one character (idk why), in edge it takes it as 2 characters so aa[enter]aa sums up to 6 characters
EDIT: 2 parts are there, one is js fix for removal of return carriage and one is php fix
js fix in html file
<!DOCTYPE html>
<head>
</head>
<body>
<form method="POST" action="test2.php">
<textarea id="input" name="input" cols="100px" rows="15" wrap="soft" placeholder="put input here"></textarea>
<button type="button" onclick="getLength()">Get length </button><p id="length"> </p></div>
<br >
<input type="submit">
</form>
<script>
function getLength(){
var fixedText = $('textarea').val();
fixedText = fixedText.replace(/(\r\n|\n|\r)/gm, "");
var len=fixedText.length;
$('#length').text(len);
}
</script>
</body>
</html>
php fix
<?php
$data = str_replace("\r\n",'',$_POST['input']);
echo strlen($data);
?>
You just need to replace the return carriages \r\n, \r and \n with "".

PHP - Can't submit form to handler

Here is my Code :
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" action="../../../wamp/www/abc.php" method="get">
<input type="text" name="text1" id="username" />
<input type="submit" />
</form>
</body>
</html>
The abc.php file is located in C:/wamp/www/abc.php
<html>
<body>
Hello World
<?php
echo "Hello";
echo $_GET["username"];
?>
</body>
</html>
In the form, when I press the Submit button, only "Hello World" is displayed.
The value entered in the textbox is not displayed at all. Even the "Hello" message printed inside the php code is not displayed.
How can i display the Value ?
text1 is the name of your input, so the correct PHP code would be.
<?php
echo "Hello ";
echo $_GET['text1'];
?>
Try the below, get array is created using name attr not id one.
echo $_GET["text1"];
There is two solution for you should use :
First Way :Change your php code:
<?php
echo "hello";
echo $_GET["text1"];
?>
Second Way: Chnage your HTML
<input type="text" name="username" id="username"/>
If second "Hello" from echo is not displayed, there is a problem with your PHP installation.

getting HTML to send content to PHP

Okay so I'm confused and lost and have no idea of what I am doing, basically here is my code:
HTML/JavaScript:
<img src="Images/Question1.png" usemap="#mainMap" id="main"/>
<map name="mainMap" id="mainMap">
<area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" />
</map
function incorrectAnswer() {
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;
document.forms["answer_send"].submit();
}
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
<input type="submit" value="Submit" />
</form>
PHP:
$id=$_POST['answer_id'];
echo $id
I have no idea why this is not working any ideas/help?
To grab values submitted via GET/POST you need to assign the name property to your input fields. Try the below:
The HTML and JS
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function incorrectAnswer() {
document.forms["answer_send"].submit();
}
</script>
</head>
<body>
<img src="none.jpg" alt="placeholder" width="200" height="200" onClick="incorrectAnswer();">
<form id="answer_send" name="answer_send" action="form.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
</form>
</body>
</html>
The PHP
<?php
$id=$_POST['answer_id'];
echo $id;
?>
Note: Your AREA element is not going to work unless you assign it to something.
Your JavaScript function must be within tags or you'll receive errors.
The HTML/PHP part of this is working fine... You are experiencing these issues due to the following three lines...
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;
where is your name attribute ?
Every input must have a name attribute to retrieve it on php
and your html code must look like this:
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/> </form>

Php Smarty: how to scrape & embed a youtube video by keyword?

please could someone let me see some examples? Really Thank You.
I found something but now i have another problem:
for example i have the following javascript inside a tpl:
<script src="http://www.domain.com/script.js" type="text/javascript"></script>
<div id="youtubeDiv"></div>
<script type="text/javascript">
insertVideos({'block':'youtubeDiv','q':'keyword','type':'search','results':8,'order':'most_relevance','player':'embed','layout':'thumbnails'});</script>
between {literal}{/literal} to let it work... but how to replace the "keyword" with the smarty variable {$product.name} ???
ok, solved... in place of 'keyword':
'{/literal}{$mySmarty_variable}{literal}'
<form onsubmit="insertVideos({'block':'youtubeDivSearch','type':'search','q':document.getElementById('ytSearchField').value,'results': 20,'order':'most_relevance','layout':'thumbnails'}); return false;">
<input id="ytSearchField" type="text" />
<input type="submit" value=" Search " />
</form>
<div id="youtubeDivSearch"></div>
<script src="http://www.yvoschaap.com/ytcp.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js" type="text/javascript"></script>
<div id="youtubeDiv"></div>
<div id="youtubeDivUser"></div>
<script>insertVideos({'block':'youtubeDivUser','type':'user','q':'','results': 20,'layout':'thumbnails','player':'embed','imgstyle':'3px solid #333'});</script>

Categories