Write text into input field with a PHP variable - php

I would like to write in an input field with a PHP variable. This is the current code I have:
JS:
<script type="text/javascript">
function reply(form, inp){
form.texta.value = "#" + inp;
}
</script>
HTML:
Reply ^
<form id="form1" name="form1" method="post" action="index.php"><input type="texta" name="texta" /></form>
This code however does not put the text of the variable in the input field. Sorry, I am a bit new to JavaScript.
Can anyone help me here?

Using my code I gave in the question and due to the answers/comments i have put single quotes around the php, and now it works perfectly. Thanks!

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

Reply ^
EDIT : You should Give Id to text box.

if you want it direct
<input type="texta" name="texta" value="<?=$poster?>" />
Or
use document.form.texta.value = value
or like this
document.forms["form"]["texta"].value = value
put any of these inside
function reply(form, inp){
}
Reply ^

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!

echo php variable string cut off after ampersand

I submit the s variable to page.php from an input form on a different page.
The URL has the complete string with the ampersand, when I try to echo the variable, the string is cut off after the ampersand.
form:
<form action="page.php" method="get">
<input type="text" name="s" id ="s" />
</form>
Variable is:
search & search
URL:
page.php?s=search+%26+search&var2=variable
variable echoes as:
search
I have tried:
echo htmlspecialchars($_GET['s']);
echo htmlentities($_GET['s']);
echo urldecode($_GET['s']);
You could use (at least) 3 differents ways to archieve this:
1 - urlencode(): use this function in the place you should generate the url to encode the string for URL format.
2 - use %26 (without the "+");
3 - Use $_SERVER['QUERY_STRING']: if you have only one param, you could get all the query_string this way.
UPDATE:
I.E:
$url = "page.php?s=".urlencode('search & search')."&var2=".urlencode('variable'); //or urlencode($variable)
Then use: urldecode() to parse the differents $_GETs.
I assume you have a code similar to this,
<a href="http://localhost/example/page.php?s=search+%26+search">
This kind, will result only the search after you try to echo it.
Try this way,
<a href="http://localhost/example/page.php?s=<?php echo urlencode("search+%26+search") ?>">
it's a convenient way to encode a query part of the url.
and also try this one, an example from the official php documentation
<?php $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
and for more information, please refer to this.
http://php.net/manual/en/function.urlencode.php
I just tried what you said and the problem doesn't seem to be the encoding of url.
Try this out https://www.google.co.in/?q=search+%26+search , Google searches for search & search, there might be a problem on page.php where you are trying to echo the variable.
Additionally, to prove that google is not doing anything special i just made two files named index.php and page.php and the result was as expected.
index.php
<html>
<body>
<form action="1.php" method="get">
<input type="text" name="s" id="s">
</form>
</body>
</html>
page.php
<?php
echo $_GET['s'];
?>
output:
search & search
Try to debug your code on page.php where you are trying to echo the variable or paste the whole code here.
Try this though I test it locally, here's the code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Problem</title>
</head>
<body>
<form action="page.php" method="get">
<input type="text" name="s" id ="s" />
<input type="text" name="var2" id ="d" />
<input type="submit" name="name" value="submit">
</form>
</body>
</html>
and page.php
<?php
if (isset($_GET['s'])) {
echo $_GET['s'] .' '. $_GET['var2'];
}
?>
when you run it to your browser, it will display two input form and a submit button.
The output will be
search & search variable
I try also pasting in the url
page.php?s=search+%26+search&var2=variable&name=submit
I result the same
I hope this would help

How to retain search query after clicking search and printing result on same page using PHP

I have created my PHP page where I have search query field. After submitting query I am printing result on same page. The query is working fine but I want query to be displayed even after result displayed. i.e. query is being disappeared after result comes. How can I retain the query word/s along with result in webpage. This might be very basic and sounds like stupid but since I am newbie and tried so many ways but in vain.
Below is my code:
<html>
<head>
<TITLE>PHP FORMS</TITLE>
</head>
<body algin=center>
<p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter Drug Name <input type="text" name="drugName"></p>
<p><input type="submit" value="search"></p>
</form>
</body>
</html>
Can anyone suggest valuable idea and cause for it?
try this
<input type="text" name="drugName"
value="<?php echo (isset($_POST['drugName']) ? $_POST['drugName'] : '') ?>">
Change your form method to GET and append as many strings as you want. Then via PHP, use $_GET['varstring'] value.
OR if you must use POST
foreach ($_POST as $set => $myval){
echo "{$set} = {$myval}\n";
}

How to populate input fields with PHP

I'm trying to write a simple calculator html page where I ask for two numbers in separate text boxes, have the user click a button and then the result gets stored into a third text box. I'm trying to using PHP to resolve this but can't seem to display it the way I want.
the echo line works fine, but I don't want that;
From HTML
<form action="Add.php" method="get">
<input for='num1' type="text" name="num1" />
<input for='num2' type="text" name="num2" />
<input type="submit" value="Add Them" />
<input type="text" name="AnswerBx" id="SumTotalTxtBx"/>
</form>
Add.php
<?php
$num1 = $_GET["num1"];
$num2 = $_GET['num2'];
$sum = $num1+$num2;
//echo "$num1 + $num2 = $sum";
document.getElementById("SumTotalTxtBx").value = $sum;
?>
You can't mix PHP and JavaScript like that! One is run on the server the other on the client.
You have to echo the value into the value attribute of the text boxes like so
<input type="text" value="<?PHP echo $sum; ?>" />
You are mixing PHP and Javascript here, go back to the PHP manual at : http://php.net/manual and read out how php works and interacts with the user.
Javascript is basicaly run on the client side while PHP is run on the server. You REQUEST php something and it returns a new HTML page for you.
That being said, its a too broad topic to help you fix your error.
Good luck
Using jQuery you can solve it without doing post back's at Server like this
var num1 = parseInt($("input:text[name='num1']"));
var num2 = parseInt($("input:text[name='num2']"));
$('input:text#SumTotalTxtBx').val(num1+num2);
Okay, so PHP is awesomeness, but all of it's calculations are performed on the serverside, not the clientside. Using AJAX, you can execute some of the PHP code back against the server, but I think you may be more interested in javascript for your calculator.
<html>
<head>
<script>
function calculateSum()
{
num1 = parseInt(document.getElementById("num1").value);
num2 = parseInt(document.getElementById("num2").value);
sum = num1 + num2;
document.getElementById("sum").innerHTML = sum;
}
</script>
<body>
<input id="num1" type="text"/><br/>
<input id="num2" type="text"/><br/>
<button onclick="calculateSum()">Submit!</button>
<span id="sum">..the sum is..</div>
</body>
</html>
Javascript is really quite simple, and is absolutely wonderful when programming anything on the clientside. If you want to learn more, I'd suggest you read up on javascript over at w3schools! I hope that helps!!

Basic problem of getting values of my textbox

I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"

Categories