I try to determine if a checkbox is checked or not, but i get an error.
test.php
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Use Proxy : <input type="checkbox" name="use_proxy"><br><br>
<input type="submit">
</form>
<?php
$use_proxy = $_POST['use_proxy'];
if ($use_proxy != "on")
{
$use_proxy = "off";
}
echo "<p> use_proxy = " . $use_proxy . "</p><br>";
?>
</body>
</html>
I get this error:
Notice: Undefined index: use_proxy in C:\xampp\htdocs\mbcl\checkbox_test.php on line 11
How can i solve it?
This is the behaviour of checkbox until they are checked , they can not be fetched at backend(PHP) . You can try as below-
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Use Proxy : <input type="checkbox" name="use_proxy" value="off"><br><br>
<input type="submit">
</form>
<?php
$use_proxy = isset($_POST['use_proxy'])?"on":"off";
echo "<p> use_proxy = " . $use_proxy . "</p><br>";
?>
</body>
</html>
Related
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.
I'm working on a code which has a form. The answer on the form needs to decide to which page it should go, but it doesn't work.
<html>
<body>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
<?php
if($_GET["graden"]>=28){
$action = "Koelbox.php";
}
else{
$action = "scrabble.php";
};
?>
</form>
</body>
</html> ```
You have to declare and initialize the variable before using it.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
First check the GET parameter, then set your $action variable accordingly and then use it in your echo.
I just initialized the variable before the form, but as far as I see the IF statement doesn't see the input in the form.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
I´ve been working on this project for a quite while and, the other day I was doing some code when I walk by this piece of code that keeps giving me headaches. So i got my html and php code all right but whenever i try to upload an image to my database, the image goes null, what am i doing wrong?
<?php include "connection.php"; ?>
<?php
$n=$_POST["num"];
$t=$_POST["texto"];
$i=$_POST["imagem"];
$img = mysql_query("SELECT imagem2 from segurancaofensiva where nmr=$n");
$file = $_FILES['imagem']['tmp_name'];
$image = addslashes(file_get_contents($file));
$count = $connect->query("SELECT COUNT(DISTINCT nmr) FROM segurancaofensiva")->fetch_row()[0];
if ($connect->connect_error){
die("Connection failed: " . $connect->connect_error);
}
$sql = "UPDATE segurancaofensiva SET texto='$t', imagem='{$image}', imagem2='{$image}' where nmr=$n" ;
$sql1 = "UPDATE segurancaofensiva SET texto='$t', imagem='$img' where nmr=$n";
if ($_FILES['imagem']['name']!=='' && $connect->query($sql) !== false )
{
if ($n <= $count) {
echo "actualizou\n\n";
var_dump($n);
var_dump($i);
var_dump($image);
}
else
{
echo "nao atualizou numero fora dos limites";
}
} else {
if ($connect->query($sql1) !== false){
echo "atualizou\n\n";
} else {
echo "errp";
}
}
$connect->close();
?>
<?php include 'connection.php'; ?>
<?php
$campo = $_POST['selected'];
$query = "SELECT campo FROM segurancaofensiva";
$result1 = mysqli_query($connect, $query);
$stored = $campo;
$obterquery = "SELECT * FROM segurancaofensiva where campo ='$campo'";
$x = $connect->query ($obterquery) or die ("Erro na variavel resultado");
$final = $x->fetch_array (MYSQL_ASSOC);
?>
<html>
<body>
<head>
<link rel="stylesheet" type="text/css" href="css/styleBO.css">
</head>
<div class="formulario" style="width: 100%; height: 100%;">
<form name="form2" method="POST" action="">
<h6>Campo:</h6> <select name ="selected" id="selected" >
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
<?php endwhile;?>
</select>
<input type="submit" id="load" class="load" name="load" value="Carregar">
<input type="hidden" name="selectedValue" value="0"/><br>
</form>
</div>
<div class="formulario" id="form2" style="width: 100%; height: 100%;">
<form name="form1" target="apresenta" method="POST" action="menu3.php">
<label> Atualizar dados </label><br>
<h6>Texto:</h6><textarea name="texto" id="texto"><?php echo htmlspecialchars($final['texto']);?></textarea><br>
<h6>Imagem:</h6><input type="file" name="imagem"><br>
<input type="hidden" value="<?php echo htmlspecialchars($final['nmr']);?>" name="num">
<input type="submit" name="submit" value="Enviar" class="topo">
<input type="reset" value="Limpar" class="topo">
</form>
</div>
</body>
</html>
Change form html like below. PHP will not detect file object without this.
<form enctype="multipart/form-data">
Add form attribute as per your requirement.
You are missing enctype attribute in form tag
enctype='multipart/form-data'
If you post data and trying to upload file you must use enctype
I am trying to submit a form on the same page and I get this error:
Undefined index: text1
And:
Undefined index: text2.
Here is to code I am using:
<?php include('includes/header.php') ?>
<section class="login">
<?php
if (isset($_POST['submitbtn'])) {
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
echo "Success! You entered: " . $text1 . "<br>";
echo "Success! You entered: " . $text2 . "<br>";
} else {
?>
<form action="" method="post">
<p><label>Text1:</label><input type="text" name"text1"/></p>
<p><label>Text2:</label><input type="text" name"text2"/></p>
<input type="submit" name="submitbtn"/>
</form>
<?php } ?>
</section>
<?php include('includes/footer.php') ?>
header.php consists of an html navigation and it opens a connection to
the database.
footer.php has the footer and closes the connection.
What am I doing wrong? Also is there a better way in submitting a form on the same page?
Forget = in name"text1". It would be
<p><label>Text1:</label><input type="text" name="text1"/></p>
<p><label>Text2:</label><input type="text" name="text2"/></p>
I have this code :
<?php
if ($_GET["do"]=="success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>
And I get this error:
Notice: Undefined index: do in C:\Program Files\EasyPHP-5.3.8.1\www\m\file.php on line 2
What do I need to do?
You need to check if the index exists before trying to access it:
<?php
if (isset($_GET["do"]) && $_GET["do"] == "success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>
Try this:
<?php
if (!empty($_GET['do']) && $_GET["do"]=="success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="get">
<input type="text"><input type="submit">
</form>