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 "".
Related
Say I have a page with a textarea which acts as an input.
Then I have a Submit button and right under everything i have the
output textarea.
Now what I want to do is when the input has been submitted and
sent into the output text area, how can I then retrieve the text from the output area.
This is the code i have:
<head>
<?php error_reporting(0);
$OutputText = $_GET['OutputText'];
?>
</head>
<body>
<form action="#" method="_GET">
<textarea name="InputText">
hi
</textarea>
<input type="submit" name="submitFirstInput">
</form>
<textarea name="OutputText">
<?php echo $_GET['InputText']; ?>
</textarea>
<hr>
<p>Output String Length: <?php echo strlen($OutputText); ?> </p>
</body>
for reasons I dont understand, it cant define the $OutputText,
Do they both have to be in a form? As i have understood form's is only to send data, and testing it didn't help much either.
Keep in mind this is just a barebones version of the original, essentially i have some Input text and then through some logic it gets modified, therefor i want some statistics for the output result. So just getting the first input isnt rather useful..
adding some javascript you can sync the two textarea:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(window).load(function(){
$("#one, #two").on("change keyup", function(){
$("textarea").not($(this)).val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText" id="one"></textarea>
<textarea name="OutputText" id="two"></textarea>
<input type="submit" name="submitFirstInput">
</form>
<hr>
<?php echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length:
<?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
Textarea must be inside the form tag, and the method must be GET (or POST)
try this:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText">hi</textarea>
<input type="submit" name="submitFirstInput">
<textarea name="OutputText"><?php echo $_GET['InputText']; ?></textarea>
</form>
<hr>
<?php //echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length: <?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
To start with: My web coding skills are nearly to non existent, I just did a bit of HTML ages ago...
And my first steps with php are just made today.
My goal is: Having a color picker and sending the picked value as an argument to an python script.
I managed to use the jscolor picker libary (http://jscolor.com/examples/) and I've been also able to run the python scripts with an (color) argument from php.
My problem is now:
How do I submit the color string (hex-str) to the exec command (LED_color)?
<?php
$LED_color="FFFFFF";
$LED_color=escapeshellarg($LED_color);
if (isset($_POST['button']))
{
$LED_color = $_REQUEST['hex-str'];
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<html>
<head>
<title>Basic usage</title>
</head>
<body>
<div style="position:absolute; left:280px; top:10px;">
toString = <span id="hex-str"></span><br />
</div>
<script src="jscolor.js"></script>
Color: <input class="jscolor {closable:true,closeText:'Close me!',onFineChange:'update(this)'}" value="000000">
<script>
function update(picker) {
document.getElementById('hex-str').innerHTML = picker.toString();
}
</script>
<form method="post">
<p>
<button name="button">Submit color</button>
</p>
</form>
</body>
</html>
Your color picker input needs a name attribute to match your PHP code, and also needs to be inside the <form> element.
<?php
if (!empty($_POST["hex-str"])) {
$LED_color = escapeshellarg($_POST["hex-str"]);
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Basic usage</title>
<script src="jscolor.js"></script>
</head>
<body>
<form method="post">
<p>
Color:
<input type="text" name="hex-str" value="000000" class="jscolor" data-jscolor="{closable:true,closeText:'Close me!',onFineChange:'update(this)'}"/>
toString = <span id="hex-str-display"></span>
</p>
<p>
<button type="submit">Submit color</button>
</p>
</form>
<script>
function update(picker) {
document.getElementById('hex-str-display').innerHTML = picker.toString();
}
</script>
</body>
</html>
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>
I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)
I'm generating a number of textareas with jQuery. I want to retrieve the values of all the filled out textareas via an on-page POST method (it's kind of a proof of concept, quick and dirty, I wouldn't do this normally), but when I hit submit, I get the value of only one, whichever is the last textarea value posted. I imagine I can do this with an array and a foreach loop, but am unsure how to do it, given all the additions of the jQuery/on-page factors complications in this endeavor. I also imagine it might have something to do with all of the generated textareas having the same name... anyone?
Here's the code-
<script type="text/javascript">
$(document).ready(function() {
$('.textadder').click(function(){
$("form").append("<p class='introText2'>Enter More Text</p><textarea rows='5' cols='20' name='textForm' class='formText2'></textarea>");
});
});/*document ready*/
</script>
<?php
if (isset($_POST['textForm']))
{
$formTxt = $_POST['textForm'];
echo $formTxt;
}
?>
</head>
<body>
<div id="wrapper">
<div id="submittedHolder"></div>
<div class="formBox">
<form method="post" action="">
<p class="introText">Please Enter Some Text</p>
<textarea rows="5" cols="20" name="textForm" class="formText"></textarea>
<input type="submit" class="submitter" value="Submit">
</form>
<div class="textadder"><p>More Text</p></div>
<div class="clearer"></div>
</div><!--formBox-->
</div><!--wrapper-->
</body>
</html>
Thanks!
Use textForm[] in your textarea name instead of textForm:
<textarea rows="5" cols="20" name="textForm[]" class="formText"></textarea>
After that all "textForm" textareas would be in $_POST['textForm'] array.
<script type="text/javascript">
$(document).ready(function() {
$('.textadder').click(function(){
$("form").append("<p class='introText2'>Enter More Text</p><textarea rows='5' cols='20' name='textForm[]' class='formText2'></textarea>");
});
});/*document ready*/
</script>
<?php
if (isset($_POST['textForm']))
{
$formTxt = $_POST['textForm'];
foreach($formTxt as $txt){
echo $txt;
}
}
?>
</head>
<body>
<div id="wrapper">
<div id="submittedHolder"></div>
<div class="formBox">
<form method="post" action="">
<p class="introText">Please Enter Some Text</p>
<textarea rows="5" cols="20" name="textForm[]" class="formText"></textarea>
<input type="submit" class="submitter" value="Submit">
</form>
<div class="textadder"><p>More Text</p></div>
<div class="clearer"></div>
</div><!--formBox-->
</div><!--wrapper-->
</body>
</html>
Use array here.
the post object will now be an array.