How to create a form that accept number only - php

How can I create a form that accept number only?
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label>x:</label><br>
<input type="text" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['xx']) || !empty($_POST['xx']) || is_numeric($_POST['xx'])){
$x = $_POST['xx'];
}else{
echo "vous devez taper des chiffres!";
}
echo $x . "<br />";
?>
</body>
</html>
I am new in PHP please an easy answer.
Thank you :-)

If you only want to accept a number, you can also do so using the html element <input>.
Like this:
<input type= "number" name= "xx">
As a precaution, check the server-side $ _POST["xx"] element. Whether the element contains the expected values.
Like this:
check whether the element has been filled in and does not contain only white characters.
And remove any white space before and after the number using trim()
$xx = trim($_POST["xx"]);
if(!empty($xx) && is_numeric($xx)) {
// if "if-condition" true
} else {
// if "if-condition" false
}

I have this message if it is empty
enter image description here
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>">
<label>x:</label><br>
<input type="number" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
$value = trim($_POST["xx"]);
if(!empty($value) && is_numeric($value)) {
$xxx = $_POST['xx'];
} else {
echo "vous devez taper des chiffres!";
}
echo $xxx . "<br />";
?>
</body>
</html>
It is not an answer this, I don't know how to add code in the comment.

Related

Form validating in PHP - error message not displayed

I am beginner in PHP and I want to check whether user has filled the input named "jmeno". Name of the corresponding variable is the same. If the input is not entered, then the variable "chybi" should be expanded by text "Zadej jméno!" and that text should appear above the form.
I am getting no errors. If the input is filled, then the form proceeds. If not, then the form doesn't proceed - that works how it's supposed to. But the error message in variable "chybi" doesn't display for some unknown reason in case that the variable "jmeno" is empty (second if).
I have tried many things. It's strange that such a simple script doesn't work. Any ideas? Thank you.
<?php
$chybi = '';
$zacatek = '
<p>some long text</p>
<form action="index.php" method="post" class="akce">
<p>' .$chybi.
'<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
<p>
vyber pohlaví<br />
<input type="radio" name="pohlavi" value="žena" /> žena<br />
<input type="radio" name="pohlavi" value="muž" /> muž
</p>
<p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
</form>
';
if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
$jmeno = $_POST['jmeno'];
$pohlavi = $_POST['pohlavi'];
if ( empty($jmeno) ) {
$chybi .= 'Zadej jméno!<br />';
echo $zacatek;
}
else {
echo "Jmenuješ se $jmeno a jsi $pohlavi.";
}
}
else {
echo $zacatek;
}
?>
As #jylipaa pointed out you're echoing $chybi before setting it's value. Move your logic above the $zacatek varaible.
<?php
$chybi = '';
if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
$jmeno = $_POST['jmeno'];
$pohlavi = $_POST['pohlavi'];
if ( empty($jmeno) ) {
$chybi .= 'Zadej jméno!<br />';
}
else {
echo "Jmenuješ se $jmeno a jsi $pohlavi.";
}
}
$zacatek = '
<p>some long text</p>
<form action="index.php" method="post" class="akce">
<p>' .$chybi.
'<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
<p>
vyber pohlaví<br />
<input type="radio" name="pohlavi" value="žena" /> žena<br />
<input type="radio" name="pohlavi" value="muž" /> muž
</p>
<p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
</form>
';
echo $zacatek;
?>
you are setting $zacatek in the start of code where $chybi is still empty. It is then handled as a string and setting the value of $chybi later on will not change the content of a string afterwards.

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

Keep text in text field after submit

I'm building a form, and I want that all the inserted values will be kept, in case of form submit failure. This is my code:
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value=""/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
I would like that name field keeps the inserted input text, after submit. I tried to do with php code in input value but doesn't work. Any ideas?
Solved. This is the solution that I was looking for.
I added in value tag of input the following:
<?php if (isset($_POST['name'])) echo $_POST['name']; ?>
Therefore input field would look like:
<input type="text" placeholder="Name" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
Thanks for your responses, helped me.
<?php
$error = "";
$name = isset($_POST["name"])?$_POST["name"]:""; //Added condition
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
You can just echo $_POST['name'] in the value attribute of the input.
Make sure you check POST values to avoid XSS.
I also put up the update DB function, as you don't need to show the form to the user if the name in longer the 4 chars!
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['name'])){ //change name content only if the post value is set!
$name = filter_input (INPUT_POST, 'name', FILTER_SANITIZE_STRING); //filter value
}
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
} else {
// Update name in DB
// Redirect
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
};
?>
</body>
</html>
If you want to keep all values/inputs for further use you could achieve that with php session values.
Besides - you should use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF']
Mars

Print the content of text area not working

php:
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
html:
<input type="submit" name="submit_" value="Add" />
<textarea name="textEditor" rows="20" cols="60" > </textarea>
I want to print the content of the text area when submit button is clicked. But even when the textarea is non-empty, it prints nothing.
For testing, I printed 'hello', but it still prints nothing that is the second ' if ' statement is not satisfied. I do not understand why does the second ' if ' statement fail !
And if I remove the second if statement, then I get an error:
Notice: Undefined index: textEditor in...
seems you have texteditor outside of form you need to try like
<?php
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
?>
<form method="post">
<textarea name="textEditor" rows="20" cols="60" > </textarea>
<input type="submit" name="submit_" value="Add" />
</form>
if form is set already try to check form method="post" or print_r($_POST); in php code
Try this:
<html>
<?php
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
?>
<head>
</head>
<body>
<form name="myForm" method="post">
<textarea name="textEditor" rows="20" cols="60" > </textarea>
<input type="submit" name="submit_" value="Add" />
</form>
</body>
</html>
Hope it helps
Might be debugging the code will help you.
as put this code under if(isset($POST['submit'])) { line
echo "<pre>";
print_r($_POST);
echo "</pre>";
hope this helps.

set value of input field by php variable's value

I have a simple php calculator which code is:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input> tag.
Try this -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
inside the Form, You can use this code. Replace your variable name (i use $variable)
<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">
Try this
<input class="qtytext-box" type="number" value= <?php echo $colll2; ?> >

Categories