I tried to catch the empty text value but it not work. May i have other way for catch empty value? this is my code. Please, help me. Sorry my bad English.
<?php
error_reporting(0);
include_once 'dbcon.php';
if(isset($_POST['save_mul'])){
$total = $_POST['total'];
for($i=1; $i<=$total; $i++)
{
$fn = $_POST["fname$i"];
$ln = $_POST["lname$i"];
if ($fn = "") {
alert('Please enter the name');
}else{
$sql="INSERT INTO users(first_name,last_name)
VALUES('".$fn."','".$ln."')";
$sql = $MySQLiconn->query($sql);
}
}?>
if ($fn = "") {
You have a assignment inside the expression. What you want is a check for an emtpy string like:
if ($fn == "") {
you are using alert which is a javascript function and you are not using comparison operator while checking empty text
<?php
if(isset($_POST['submit'])){
echo 'submit entered'.'<br>';
if($_POST['name'] == '')
{
echo 'enter name';
}
else {
echo $_POST['name'];
}
}
?>
<form method="POST" action="">
<input type="text" name="name" >
<input type="submit" name="submit">
</form>
It is also possible to use empty
for comparison and echo
for alert message in PHP inside the for loop
if (empty($fn)) {
echo 'Please enter the name';
}else{
$sql="INSERT INTO users(first_name,last_name)
VALUES('".$fn."','".$ln."')";
$sql = $MySQLiconn->query($sql);
}