how to post special character and symbol using php - php

I am new one in php.I was just create a form for sending special character and symbol using .I got for get the text
<form name='form1' method='post' action='s.php'>
<textarea name='ask' id='ask' style=' resize:none ; border: #093; height:250px; width:350px'></textarea><input type="submit" name="button" id="button" value="Submit" /></td>
</form>
this is form and s.php is
<?php
$ask=$_POST['ask'];
echo $ask;
?>
inputed value
#include<iostream.h>
but i got only
#include
why did i got output like this ? and any method to get full text . Please help me anyone

Right click and view the page source. The <iostream.h> is being interpreted as an HTML tag by your browser.
Passing the value through htmlentities() will make it work as expected:
$ask = $_POST['ask'];
echo htmlentities($ask);

Related

How do you style input that PHP posted with a CSS selector?

I want to create a webpage that posts onto itself text inserted into an input field, using CSS to stylize said text once it becomes part of the page. However, I don't know how to refer to it with a CSS selector. I've done what every HTML-newb tries when encountering a problem and wrapped both the form code and PHP statement in classified DIVs, however, the computer visibly doesn't know what I'm trying to address. Likewise, wrapping the PHP statement in paragraph tags doesn't apply to it the stylization said tags are associated with.
For Reference, Form & PHP Code:
<form method = "post">
<textarea name="input"></textarea>
<input type="submit">
</form>
<?php echo $_POST['input'];?>
My apologies if the solution to this is obvious; I can't find information addressing it.
you could inline style it or with a class.
<form method = "post">
<textarea name="input"></textarea>
<input type="submit" name="submit">
</form>
<div class="input-value">
<?php
if(isset($_POST['submit])) {
echo $_POST['input'];
}
?>
</div>
use .input-value class in css
Please try this!
<form method = "post">
<textarea name="input"></textarea>
<input type="submit">
</form>
<?php echo "<div style='color:red;font-family:verdana;font-size:300%'>" .
$_POST['input'] . "</div>" ?>
Are you looking for this? Please let me know! Thanks!

PHP Autofill or something?

I have seen many questions related to auto fill, but none of them worked for me. I'm not even sure if a solution to my problem lies in HTML or PHP. I am new in both of them and I'm still not used to them. I'm working on a simple chat app. What I have now is chat window, text field, and nick name field. Both of them pass the values to the text file (which is how I want this to work, no change here). Problem is that both these fields work as a form, and each time I submit, nick name field refreshes. What I would want is auto completing nickname field so it stays the same (it'd be the best if it stayed even after browser refreshes, but it will be okay if it only goes through the form submit)
code if needed:
PHP:
<?php
$action = $_GET["action"];
$myText = $_POST["mytext"];
$nick = $_POST["nick"];
if($action = "save") {
$targetFolder = "/var/www/html/xami/";
file_put_contents($targetFolder."htmlinput.txt", $nick.">".$myText);
}?>
HTML:
<form action="?action=save" name="myform" method="post">
<label for="nick">Nick:</label>
<input type=text id="nick" name="nick" placeholder="Nick" value="Name" required><br>
<input type=text name="mytext" placeholder="Text" required>
<input type="submit" value="Send.">
</form>
I was fooling around with autocomplete but no positive results.
I'm leaving post for tomorrow, I'll reply then.
Assuming your HTML is on a .php page you could have...
if(0<strlen($nick)){
echo "<input type=hidden id='nick' name='nick' value='$nick'>";
}else{
echo "<input type=text id='nick' name='nick' placeholder='Nick' value='Name' required><br>";
}
Or, use AJAX as #Rishi says.

HTML + PHP how to prevent webpage from displaying anything until submit is pressed

I am new to both HTML and PHP and I encountered a problem when working on some simple projects. Lets say I have a text bar on my webpage and I want to display the text written in text bar on webpage after the user enters some text and presses the submit button. My problem is that the webpage shows the output when the webpage first loads. Is there a way to prevent the php code from executing untill the submit is pressed ?
Here is a sample code code that indicates the problem I am referring to.
<html>
<body>
<form action="./index.php" method="GET">
First Name: <input type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
</body>
</html>
<?php
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
?>
So "This was typed into text bar" is outputted right away when website loads.
I want it to be outputted only after submit button is pressed.
Thanks.
you need to split it up the form should be showed if nothing is submited so either check the value or the submit button
make sure you keep the html format. look at label tags to describe form inputs
<html>
<body>
<form action="./index.php" method="GET">
<label for="first">First:</label>
<input id="first" type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
<?php
if (!empty($_GET['first'])) {
//take care you escape things never output user input (XSS)
$op = htmlspecialchars($_GET['first']);
echo "This was typed into text bar" . $op;
}
?>
</body>
</html>
Check if $_GET['first'] exists. This is is usually done as the following:
View
<form action="index.php" method="post">
<!-- input fields here -->
<input type="submit" name="submit" value="GO"/>
</form>
Controller
<?php
if (isset($_POST['submit'])) {
// process post
} else {
// display the form
}
<?php
if(isset($_GET['submit'])){
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
}
?>

use php variable in php file()

I have a web site that allows people to upload a csv file and then it loads it into a postgres database. uploading the file is fine and i capture the file name and location ../Data/Uploads/mycsv.csv as $_POST['fname'].
I'm trying to use this variable in $file=file($_POST['fname']) but cant get it to work however if i hard code it in as $file=file("../DATA/Uploads/mycsv.csv") it works. I have attached the code in question. Thanks in advance for any help
Also to clarify echo $_POST['fname']; returns ../DATA/Uploads/mycsv.csv, which is the same as the hard coded value.
please bear with me as im only relatively new to this. I have attached the 2 html forms being used as well. the top one passes the $fname variable containing the file name and path from the php code used to upload the file.
<Form Method="post" Action="../PHP/Loadcsv.php">
<input type="text" value="<?php echo htmlspecialchars($fname);?>" name="fname">
<br />
<Input Type="submit" Value="Continue">
</Form>
this is the php copy the csv into the database
<?PHP
if ($_POST['submit']) {
$file = file(printf($_POST['fname'])); //****doesnt work******
//$file = file("../DATA/Uploads/csv_trial1.csv"); //********This works******
$db = pg_connect("host=localhost dbname=blah user=me password=you");
pg_exec($db, "COPY personaldetails FROM stdin");
foreach ($file as $line) {
$tmp = explode(",", $line);
pg_put_line($db, sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", $tmp[0], $tmp[1], $tmp[2], $tmp[3], $tmp[4], $tmp[5], $tmp[6], $tmp[7]));
}
pg_put_line($db, "\\.\n");
pg_end_copy($db);
?>
below is the html to run the above php.
<form id='form' method='post' action='' >
<input type="submit" name="submit" />
</form>
after running a whole lot of echo to find where the variable is reaching, i dont think it is reaching the inside of the if statement possibly due to the next use of post??
**update**
So after a little playing and bouncing ideas almost literally off my office walls.... i was on the right track and Devon was right too, my problem was the 2 post requests the answer was to have a php variable $filename = $_POST['fname']; to take the variable from the first form and put this into the input for the second form
<form id='form' method='post' action='' >
<input type="hidden" value="<?php echo htmlspecialchars($filename);?>" name="fname">
<input type="submit" name="submit" />
I'm sure there may be other ways to achieve this but at the moment it works.
I'm not sure where you came up with printf(), but any print or echo command will output the arguments to the browser and won't return it to the function at hand. You don't need to use anything special to use a variable as an argument. Just: file($_POST['fname']);
Printf specifically outputs a formatted string and returns the length of the string. So this is the equivalent of calling file(integer) where integer is the length of $_POST['fname']'s value.

PHP Echo Styling output

<?php
if (in_array($key,$following)){
echo ' <form id="follow" action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</form>';
?>
I am trying to style this echo section. I have it outputting to another PHP file that is framed in HTML div tags. How would I go about this? I have everything else on the page styled correctly except for this output.
Edit** I edited the code, that's a form. But anyway, I have another php file that is referencing this php code and on the 2nd php file it is wrapped in html and the div tag on that page shows the echo from this page but I can't get the css to work on that other file. And I have tried #follow. no luck.
create your own function:
function myecho($text){
echo "<div class='your_css_class'>".$text."</div>";
}
And when you want to echo something just write myecho("my message");

Categories