i made a form where you can put a number and show which one is the highest.
i want to make sure you can put any words in this form.
that there show a notification you cant put words in the form
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="text" name="tweedegetal" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
The following code shows a notification when a letter is typed.
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" id="eerstegetal" name="eerstegetal" oninput="Eerste_check()" placeholder="Eerste getal"><br>
<input type="text" id="tweedegetal" name="tweedegetal" oninput="Tweede_check()" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
function Eerste_check(){
var eerstegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(eerstegetal)){
alert('Eerstegetal should contain only numbers.')
}
}
function Tweede_check(){
var tweedegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(tweedegetal)){
alert('Tweedegetal should contain only numbers.')
}
}
</script>
Try the following:
<input type="number" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="number" name="tweedegetal" placeholder="Tweede getal"><br>
I hope it helps
Have this code i want to check if record exist in $pas before processing result
then try to verify $pas by form input
I have tried this:
$pas = "1234";
$text = "12345678910111213141516";
if(empty($pas)) {
echo $text;
}
else
{
if (isset($_POST["subscribe"]))
{
$phone = $_POST["phone"];
if ($phone == $pas)
{
echo $text;
}
else
{
if ($phone != "$pass")
{
echo erro;
}
}
}
}
if(!isset($_POST["phone"]) || $_POST["phone"] != $pas)
{
echo '<form action="#" method="POST">
Your Phone Number <br />
<input type="text" name="phone" value="080"/>
<input type="submit" name="subscribe" value="SEND PAYMENT"/>
</form>';
}
But I get result even when 'pas' is not there what am I doing Wrong?
<?php
$pas = "1234";
$text = "12345678910111213141516";
if(empty($pas)) {
echo $text;
}
else
{
if (isset($_POST["subscribe"]))
{
$phone = $_POST["phone"];
if ($phone == $pas)
{
echo $text;
}
else
{
if ($phone != "$pass")
{
echo erro;
}
}
}
if(!isset($_POST["phone"]) || $_POST["phone"] != $pas)
{
echo '<form action="#" method="POST">
Your Phone Number <br />
<input type="text" name="phone" value="080"/>
<input type="submit" name="subscribe" value="SEND PAYMENT"/>
</form>';
}
}
Your else bracket was closing sooner than you wanted so your echo would be executed anyway as it was outside of your if/else
I'm trying to pass $_POST info as $_SESSION but when it doesn't work, I don't know what is wrong in my code.
<?php
session_start();
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
?>
<html>
<form action="accion.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
Second file
<?php
session_start();
if(isset($_SESSION['nombre']) && isset($_SESSION['edad'])) {
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
The following code will solve your problem:
File1.php
<html>
<form action="File2.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
<?php
echo $_GET['msg'];
?>
File2.php
<?php
session_start();
if(isset($_POST['nombre']) && isset($_POST['edad'])) {
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
header("Location:File1.php?msg=There was an error writing this file");
}
else {
header("Location:File1.php?msg=$ret bytes written to file";
}
}
else {
header("Location:File1.php?msg=no post data to process");
}
?>
When the form is submitted, it is submitted to File2.php
So, your first session being set and taking values as POST from it in File1.php won't work.
The best option is to set the session in the second file and then give back a message to the File1.php as shown above.
Check the code bellow. It is better to start session once at the begining of the file and check if post variable contains value to set in the session.
<?php
session_start();
if(isset($_POST['nombre']) && isset($_POST['edad'])) {
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
}
?>
<html>
<form action="accion.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
<?php
if(isset($_SESSION['nombre']) && isset($_SESSION['edad'])) {
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
If you want to send the form in the same page instead using action="action.php" use action=""
I have a list of urls that I want to extract the email or from the href or from the text. Each page has one email only.
The problem is that my list is big and can not do it manually.
EMAIL
How can I do this using PHP, regex?
/mailto:([a-zA-Z0-9_\.-]+#[\da-z\.-]+\.[a-z\.]{2,6})"/gm
see this demo https://regex101.com/r/mC7jM3/1
Extract email from url contain
<?php
$the_url = isset($_REQUEST['url']) ? htmlspecialchars($_REQUEST['url']) : '';
if (isset($_REQUEST['url']) && !empty($_REQUEST['url'])) {
$text = file_get_contents($_REQUEST['url']);
}
elseif (isset($_REQUEST['text']) && !empty($_REQUEST['text'])) {
$text = $_REQUEST['text'];
}
if (!empty($text)) {
$res = preg_match_all(
"/[a-z0-9]+([_\\.-][a-z0-9]+)*#([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i", $text, $matches
);
if ($res) {
foreach (array_unique($matches[0]) as $email) {
echo $email . "<br />";
}
}
else {
echo "No emails found.";
}
}
?>
<form method="post" action="">
Please enter full URL of the page to parse (including http://):<br />
<input type="text" name="url" size="65" value="<?php echo $the_url; ?>"/><br />
<input type="submit" name="submit" value="Submit" />
</form>
I'm writing a function that takes the content from $_POST, inserts it in a string and then returns the resulting string
To the question "What is your favorite color?" the user inputs blue
To the question "What is your favorite animal?" the user inputs dog
$content = "The visitor's favorite color is {color}";
$content = sentenceBuilder($content);
$content = "The visitor's favorite animal is a {animal}";
$content = sentenceBuilder($content);
function sentenceBuilder($content){
global $_POST;
foreach($_POST as $key => $value){
if($key=='color'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
if($key=='animal'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
}
return $content;
}
This returns "The visitor's favorite color is blue" and "The visitor's favorite animal is a dog." If they leave the color element blank, it returns "The visitor's favorite color is " and "The visitor's favorite animal is a dog". If they leave both elements blank, it returns 2 incomplete sentences.
So, I tried to modified it to say so that if $value was empty, the function would just skip it and move on to the next (as this uses every form element that moved over in the $_POST)...
if($key=='color' && $value!=''){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}else{
$content ='';
}
if($key=='animal' && $value!=''){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}else{
$content ='';
}
With this added, the result I get is blank. No sentences or anything. Even if they fill out the elements, the result is still blank with this code added.
So I tried this instead.
function sentenceBuilder($content){
global $_POST;
foreach($_POST as $key => $value){
if(isset($value) && $value!=''){
if($key=='color'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
if($key=='animal'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
else{
$content = '';
}
}
return $content;
}
This yielded the same results.
TLDR;
I want to be able to have this function replace content with values that are not empty with a sentence. The ones that are empty, I would like for the content to be not displayed.
UPDATE
I got the code to work. Had to redesign it to make it happen though.
<?php
if(isset($_POST)){
$content = "The visitor's favorite color is {color}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor's favorite animal is a {animal} from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from Iowa";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {state}";
echo sentenceBuilder($content);
}
function sentenceBuilder($content){
preg_match("/\{(.*?)\}/", $content, $checkbrackets);
if(!empty($checkbrackets)){
$gettext = str_replace('{', '', $checkbrackets[0]);
$gettext = str_replace('}', '', $gettext);
if(array_key_exists($gettext,$_POST)){
if(!empty($_POST[$gettext])){
$content = preg_replace("/\{($gettext+)\}/", $_POST[$gettext], $content);
}else{
$content = '';
}
}
}
return $content;
}
?>
<form method="post" action"sentencebuilder.php">
<input type="text" name="color" />
<input type="text" name="animal" />
<input type="text" name="land" />
<input type="submit" />
</form>
Thanks for the help Guys. Enter it in and you'll see what I was going for. I am currently working on it to change additional brackets that exist within the question.
Use this code, definitely that's the solution of you problem:-
<?php
$main_content = array();
if(isset($_POST['submit'])) {
unset($_POST['submit']);
$main_content['color'] = "The visitor's favorite color is {color}";
$main_content['dog'] = "The visitor's favorite dog is {dog}";
$main_content['school'] = "The visitor's favorite school is {school}";
$formData = array_filter($_POST);
if(!empty($formData)) {
echo sentenceBuilder($formData, $main_content);
}
}
function sentenceBuilder($formData=null, $main_content=null){
$newContent = "";
foreach ($formData as $key => $value) {
$newVal = "";
$newVal = preg_replace("/\{($key+)\}/", $value, $main_content[$key]);
$newContent .= $newVal.". <br/>";
}
return $newContent;
}
?>
<form method="post" action="#">
<input type="text" name="color" placeholder="color" />
<input type="text" name="dog" placeholder="dog" />
<input type="text" name="school" placeholder="school" />
<input type="submit" name="submit" />
**OUTPUT:**
The visitor's favorite color is RED.
The visitor's favorite dog is BULLDOG.
The visitor's favorite school is CAMPUS SCHOOL.
Hope this will help you
<?php
$content = '';
echo $content = sentenceBuilder($content);
function sentenceBuilder($content){
global $_POST;
$content = "The visitor's favorite color is {key}";
foreach($_POST as $key => $value){
if (isset($_POST[$key]) && ($_POST[$key] != '')) {
$content = preg_replace("/\{(key+)\}/", $value, $content);
}
}
if(strpos($content, '{key}') !== false)
return $content='';
else
return $content;
}?>
<form method="post">
<input type="text" name="color" />
<input type="text" name="gh" />
<input type="submit" />
</form>
If you want first color check then try
if (isset($_POST['color']) && ($_POST['color'] != '')) {
$content = preg_replace("/\{(key+)\}/", $value, $content);
}
elseif (isset($_POST[$key]) && ($_POST[$key] != '')) {
$content = preg_replace("/\{(key+)\}/", $value, $content);
}
I understand that you really need the function and not just 'inline' code. In that case, this function will do the trick (if I understand your wishes correctly).
In the comments of the code you will find some guidance.
Hope this helps :)
function sentenceBuilder($content = '')
{
preg_match_all('/\{(.*?)\}/', $content, $matches); // Find all {...} matches
$valueMissing = false; // If a POST value is missing, this will be set to TRUE
if(isset($matches[0]) && !empty($matches[0])) { // Braces are found
foreach($matches[0] as $id => $match) {
// Note the usage of $matches[0] vs $matches[1]: $matches[0] = '{animal}','{land}', while $matches[1] = 'animal','land' without the braces
$key = (isset($matches[1][$id]) ? $matches[1][$id] : false); // Quick if/else
$postValue = ($key != false ? (isset($_POST[$key]) ? $_POST[$key] : false) : false); // Quick if/else (double)
if($postValue == false) {
$valueMissing = true;
break; // Leave the foreach loop
} else {
$content = str_replace($match, $postValue, $content); // Replace the value
}
}
if($valueMissing) {
return ''; // Return empty string (braces found, but not all values were found in the POST)
}
}
return $content; // Return the content
}
-------- ORIGINAL POST --------
The preg-replace seems a bit overkill for this, isset() and empty() will do the trick.
<?php
if(isset($_POST) && !empty($_POST))
{
// Start with empty content
$content = '';
// Only add content (.=) when the field is set (posted) and NOT empty
if(isset($_POST['color']) && !empty($_POST['color'])) {
$content .= "The visitor's favorite color is " .$_POST['color']. "<br/>";
}
if(isset($_POST['animal']) && isset($_POST['land']) && !empty($_POST['animal']) && !empty($_POST['land'])) {
$content .= "The visitor's favorite animal is a " .$_POST['animal']. " from " .$_POST['land']. "<br/>";
}
if(isset($_POST['state']) && !empty($_POST['state'])) {
$content .= "The visitor is from " .$_POST['state']. "<br/>";
}
if(isset($_POST['number']) && !empty($_POST['number'])) {
$content .= "The visitor's favorite number is " .$_POST['number']. "<br/>";
}
// If there is some content, show it
if(!empty($content))
{
echo $content;
}
else
{
echo "Please fill in some values!";
}
}
else
{
?>
<form method="post" action"<?php echo $_SERVER['PHP_SELF']; ?>">
Favorite color: <input type="text" name="color" /><br />
Favorite animal: <input type="text" name="animal" /><br />
from land: <input type="text" name="land" /><br />
<br />
Favorite number: <select name="number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="SEND this form" />
</form>
<?php }
And you can use HTMLPurifier (for example) to 'filter' any user-input data. http://htmlpurifier.org/
This works.
<?php
if(isset($_POST)){
$content = "The visitor's favorite color is {color}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor's favorite animal is a {animal} from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from Iowa";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {state}";
echo sentenceBuilder($content);
}
function sentenceBuilder($content){
preg_match("/\{(.*?)\}/", $content, $checkbrackets);
if(!empty($checkbrackets)){
$gettext = str_replace('{', '', $checkbrackets[0]);
$gettext = str_replace('}', '', $gettext);
if(array_key_exists($gettext,$_POST)){
if(!empty($_POST[$gettext])){
$content = preg_replace("/\{($gettext+)\}/", $_POST[$gettext], $content);
}else{
$content = '';
}
}
}
return $content;
}
?>
<form method="post" action"sentencebuilder.php">
<input type="text" name="color" />
<input type="text" name="animal" />
<input type="text" name="land" />
<input type="submit" />
</form>