I have little experience with PHP and regular expressions. I created a simple html input form and a submit button. i would like to do the following:
When the button is clicked, regexp splits the input string into an array of words:
that are all lowercase (with all ã changed to a)
and are all 10 symbols long.
Should it be like that?
$search_string = $_GET['keywords'];
$regexp = preg_split(^[a-zA-Z]{1,10}+$, $search_string);
$regexp = strtolower($search_string);
This is my simple html code:
<div>
<label for="keywords">keywords:</label>
<input type="text" name="keywords">
</div>
<input type="submit" value="Add Records">
Can you help me improve my regexp code? It doesn't seem to be working.
This is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="htmlform.php" method="POST">
keywords <input type="text" name="keywords">
<input type="submit" value="submit">
</form>
<?php
$search_string = $_GET['keywords'];
$regexp = preg_split("/^[a-zA-Z]{1,10}+$/", $search_string);
$regexp = strtolower($search_string);
?>
</body>
</html>
Maybe it is because your form method is POST but you are looking at $_GET?
$search_string = $_POST['keywords'];
Related
I have the code but it doesn't seem to work and I don't know why.
I don't know what the this.php is for.
here is the instruction:
Create a form with one input that accepts text.
Once the user submits the form, their original input should be shown in the input field and the number of words in their input should be shown below the form.
Display an error message if the user submits no input.
As a bonus, also display the number of characters in the input below the form.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body class="container">
<h1>Word Counter</h1>
<form action="this.php" method="post" class="form">
<textarea name="input" class="form-control" style="width:50%">
<?php
if(!empty($_POST['input'])){
echo $_POST['input'];
}
?>
</textarea><br />
<input type="submit" class="btn btn-primary" value="Count!" />
</form>
<?php
if(!empty($_POST['input'])){
echo "<p><strong>Output:</strong><br>Number of Words: $words<br>Number of Characters: $chars</p>";
}
?>
</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 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 "".
I looked around Stack Overflow for the recommended method type for PHP; either the GET or POST method. Recommended by the community, the GET method seems to be a good idea for passing queries for a simple search engine.
Unfortunately, the GET method adds unnecessary addition symbols to the URL to indicate spaces. Basically, a aesthetic issue.
I tried the trim() function to lessen the spaces, however, that is only called after the data is submitted and the URL is already set with the parameters.
Here is a the index.php file I think is not cooperating with me.
<?php $query = ""; ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="index.php" method="GET">
<input type="text" name="query" placeholder="Enter Query">
<input type="submit" text="Search">
</form>
<?php
$query = $_GET['query'];
print $query;
?>
</body>
</html>
A example, if needed. If I type into the search bar this query...
sample 1
The URL will be formed this way...
http://localhost/search/index.php?query=sample++++++++++++++1
Is there a way to fix this problem or is the POST method the only way to circumvent this problem?
You will need to use the POST method.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="index.php" method="POST">
<input type="text" name="query" placeholder="Enter Query">
<input type="submit" text="Search">
</form>
<?php
$query = $_GET['query'];
print $query;
?>
</body>
</html>
Use urlencode or str_replace.
urlencode will replace all spaces with plus symbols, and with str_replace you can replace either underscores with plus symbols, or spaces with minus symbols.
Replace spaces with underscores: str_replace(' ', '_', $url);
Urlencode your $_GET*: urlencode($url);
I have the main document created with DOM server-side using PHP. And I have to include some pieces of PHP/HTML without DOM.
Example. There is file form.php containing a form
<form action="" method="post">
<input type="text" name="firstname" id="firstnameid" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
I want to place that form into a <DIV> created using DOM in the file index.php. The DIV inside depends on other logic (condition $form_is_needed)
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8" /> </head>
<body>
<?php
$form_is_needed = true;
$dom = new DOMDocument();
$domDiv = $dom->createElement("div");
if ($form_is_needed) {
// trying load not DOM
$str = file_get_contents('form.php');
$domDivText = $dom->createTextNode( html_entity_decode( $str ) );
$domDiv->appendChild($domDivText);
}
else {
$domDivText = $dom->createTextNode( "There is no form." );
$domDiv->appendChild($domDivText);
}
$dom->appendChild($domDiv);
$html = $dom->saveHTML();
echo $html;
?>
</body>
</html>
I get my content inside DIV but the escape characters has been added
<div><form action="" method="post">
<input type="text" name="firstname" id="firstnameid" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
There is, to my knowledge, no node which can emit raw content, but if it is well-formed, you could try loading it into a DOMDocumentFragment and appending it that way:
$frag = $dom->createDocumentFragment();
$frag->appendXML($str);
$domDiv->appendChild($frag);
Replace
$str = file_get_contents('form.php');
With
$str = readfile('form.php');
That should do it.