Below is the code I have, which checks for if text box is empty then give warning beside text box once clicked on Submit.
<?php
if(isset($_POST["submit"])) {
$fqdn = $_POST["fqdn"];
$ip = $_POST["ip"];
$fileText = $fqdn."\n".$ip;
$file = fopen("inputFile.txt","w");
fwrite($file, $fileText);
fclose($file);
}
?>
<form action = "<?php $_PHP_SELF ?>" method = "post">
<table style= "width:400px">
<tr class="spaceUnder">
<td><b>FQDN:</b></td>
<td><input type="text" name="fqdn" placeholder="server.domain.com"/></td>
<td> <?php if(isset($_POST['fqdn']) && $_POST['fqdn'] == ''){ echo "<font color='red'>FQDN cannot be empty</font>";} ?> </td>
</tr>
<tr class="spaceUnder">
<td><b>IP:</b></td>
<td><input type="text" name="ip" placeholder="***.***.***.***"/></td>
<td> <?php if(isset($_POST['ip']) && $_POST['ip'] == ''){ echo "<font color='red'>IP cannot be empty</font>"; } ?> </td>
</tr>
<tr>
<td align="center" colspan="2">
<input type = "submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
After clicking on submit, the php is still going and writing to the file. If i give exit() or return false; at the below step:
<td> <?php if(isset($_POST['fqdn']) && $_POST['fqdn'] == ''){ echo "<font color='red'>FQDN cannot be empty</font>"; exit();} ?> </td>
the form becomes incomplete, means, the IP textbox and submit button will not exist. Any way to make it right?
So what we have here. We are making 2 variables that will dispay our errors. The variable in first place are emty because we dont know what we have post. After we post the data we can see if the post is emty or not. If the post is emty we assign an error message to the variable and dispay it to our table.
$error_FQDN = "";
$error_ip = "";
if(isset($_POST["submit"])) {
if($_POST["fqdn"] == "" || $_POST["ip"] == ""){
if($_POST["fqdn"] == ""){
$error_FQDN = "FQDN cannot be empty!";
}
if($_POST["ip"] == ""){
$error_ip = "IP cannot be empty!";
}
} else {
$fqdn = $_POST["fqdn"];
$ip = $_POST["ip"];
$fileText = $fqdn."\n".$ip;
$file = fopen("inputFile.txt","w");
fwrite($file, $fileText);
fclose($file);
}
}
<form action = "<?php $_PHP_SELF ?>" method = "post">
<table style= "width:400px">
<tr class="spaceUnder">
<td><b>FQDN:</b></td>
<td><input type="text" name="fqdn" placeholder="server.domain.com"/></td>
<td><?php echo $error_FQDN ?></td>
</tr>
<tr class="spaceUnder">
<td><b>IP:</b></td>
<td><input type="text" name="ip" placeholder="***.***.***.***"/></td>
<td><?php echo $error_ip ?></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type = "submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
Try this,
// Check for form submit and if fields aren't empty
if(isset($_POST["submit"]) && $_POST['fqdn'] != "" && $_POST['ip'] != "") {
$fqdn = $_POST["fqdn"];
$ip = $_POST["ip"];
$fileText = $fqdn."\n".$ip;
$file = fopen("inputFile.txt","w");
fwrite($file, $fileText);
fclose($file);
}
By validating fqdn and ip, if values aren't empty, if case will be executed. Else it simply won't run.
Related
So I am trying to get the title from the URL by using $_GET['title'] in the first PHP file, but I can't get the file on the 2nd file.
URL:
https://easy2book.000webhostapp.com/neworder.php?bookid=101&title=SENIOR%20secondary%20geography%20fieldwork%20and%20assessment%20practice%202021.%20For%20HKDSE%202021%20/%20Ip%20Kim%20Wai%20...%20[et%20al.].
1st File:
<?php
include_once 'header.php';
$id2 = mysqli_real_escape_string($conn, $_GET['bookid']);
$title2 = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="neworder-form">
<h2>Order</h2>
<div class="neworder-form-form">
<form action="neworder.inc.php" method="post">
<table>
<tr>
<td>Book ID:</td>
<td>
<input type="text" disabled="disabled" name="bookid2" value="<?= $id2 ?>">
</td>
</tr>
<tr>
<td>Book Title: </td>
<td>
<input type="text" disabled="disabled" name="title2" value="<?= $title2 ?>">
</td>
</tr>
<tr>
<td>Username: </td>
<td>
<input type="text" name="uid2" placeholder="Username...">
</td>
</tr>
<tr>
<td>Comfirmed Book ID: </td>
<td>
<input type="text" name="id2" placeholder="Please enter the Book ID....">
</td>
</tr>
</table>
<button type="submit" name="submit2">Order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput2") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "usernametaken2") {
echo "<p>Username already taken!</p>";
}
}
?>
</section>
2nd File:
<?php
if (isset($_POST["submit2"])) {
// First we get the form data from the URL
$uid2 = $_POST["uid2"];
$id2 = $_POST["id2"];
$title2 = $_POST["title2"];
// Then we run a bunch of error handlers to catch any user mistakes we can (you can add more than I did)
// These functions can be found in functions.inc.php
require_once "dbh.inc.php";
require_once 'functions2.inc.php';
// Left inputs empty
// We set the functions "!== false" since "=== true" has a risk of giving us the wrong outcome
if (emptyInputOrder2($uid2,$id2) !== false) {
header("location: ../neworder.php?error=emptyinput&bookid=$id2&title=$title2");
exit();
}
// Is the username exists
if (uidExists2($conn, $uid2) !== true) {
header("location: ../neworder.php?error=undefineuser");
exit();
}
// If we get to here, it means there are no user errors
// Now we insert the user into the database
createUser($conn, $uid2, $id2);
} else {
header("location: ../neworder.php");
exit();
}
The input fields are disbled, disabled inputs are not posted.
Replace $title2 = $_POST[""]; with $title2 = $_POST["title2"];
This question already exists:
Why does the hash function not work no matter where i put it? [closed]
Closed 1 year ago.
I´m trying to get this form right but i can´t seem to find out whats wrong.
No matter where i put the hash in the code i can´t seem to get it to work.
I tried to get the input out from the form to convert it to a hash with password_hash(#psw, PASSWORD_DEFAULT) but no matter where i put it it won´t work.... do i need to say i am a newbie at this =)
if(isset($_POST["uname"]) && isset($_POST["psw"]))
{
// check if user exist.
$file=fopen("log.txt","r");
$finduser = false;
while(!feof($file))
{
$line = fgets($file);
$array = explode(";",$line);
if(trim($array[0]) == $_POST['uname'])
{
$finduser=true;
break;
}
}
fclose($file);
// register user
if( $finduser )
{
echo $_POST["uname"];
echo ' fanns redan registrerad!';
}
else
{
$file = fopen("log.txt", "a");
//funkar ej
$psw = password_hash($_POST['psw'], PASSWORD_DEFAULT);//funkar ej
fputs($file,$_POST["uname"].";".$_POST["psw"]."\r\n");
fclose($file);
echo $_POST["uname"];
echo " är nu registrerad!";
}
}
if(isset($_POST['login'])) // it checks whether the user clicked login button or not
{
$uname = $_POST['uname'];
$psw = $_POST['psw'];
if(isset($_POST["uname"]) && isset($_POST["psw"])){
$file = fopen('log.txt', 'r');
$good=false;
while(!feof($file)){
$line = fgets($file);
$array = explode(";",$line);
if(trim($array[0]) == $_POST['uname'] && trim($array[1]) == $_POST['psw']){
$good=true;
header("Location:index.php");
break;
}else{
echo "Ogiltigt användarnamn eller lösenord";
}
}
fclose($file);
}
}
?>
<html>
<head>
<title> Registreringssida </title>
</head>
<body>
<form action="" method="post">
<table width="200" border="0">
<tr>
<td>Användarnamn:</td>
<td> <input type="text" name="uname" > </td>
</tr>
<tr>
<td>Lösenord:</td>
<td><input type="password" name="psw"></td>
</tr>
<tr>
<td> <input type="submit" name="reg" value="Ny Användare"></td>
<td></td>
</tr>
</table>
</form>
<form action="" method="post">
<table width="200" border="0">
<tr>
<td>Användarnamn:</td>
<td> <input type="text" name="uname" > </td>
</tr>
<tr>
<td>Lösenord:</td>
<td><input type="password" name="psw"></td>
</tr>
<tr>
<td> <input type="submit" name="login" value="Logga In"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>```
Ok, so I almost have this script finished, but I'm having an issue where I am getting an Undefined Index error when I try to submit the page on the 'site' index, used in both the $_GET to get it from the URL, and the submit.
<?php
// including the database connection file
include_once("config.php");
$sitenumber = "";
$videolink = "";
$daynight = "";
$maxtents = "";
$maxpersons = "";
$geography = "";
$view = "";
$forestcover = "";
$waterfront = "";
$firepit = "";
$description = "";
$reslink = "";
if(isset($_POST['update']) && isset($_GET['site']))
{
$sitenumber = $_POST['sitenumber'];
$videolink = $_POST['videolink'];
$daynight = $_POST['daynight'];
$maxtents = $_POST['maxtents'];
$maxpersons = $_POST['maxpersons'];
$geography = $_POST['geography'];
$view = $_POST['view'];
$forestcover = $_POST['forestcover'];
$waterfront = $_POST['waterfront'];
$firepit = $_POST['firepit'];
$description = $_POST['description'];
$reslink = $_POST['reslink'];
// checking empty fields
if(empty($sitenumber) || empty($videolink) || empty($daynight) ||
empty($maxtents) || empty($maxpersons) || empty($geography) ||
empty($view) || empty($forestcover) || empty($waterfront) ||
empty($firepit) || empty($description) || empty($reslink)) {
if(empty($sitenumber)) {
echo "<font color='red'>Site Number field is empty.</font><br/>";
}
if(empty($videolink)) {
echo "<font color='red'>YouTube Link field is empty.</font><br/>";
}
if(empty($daynight)) {
echo "<font color='red'>Day or overnight field is empty.</font>
<br/>";
}
if(empty($maxtents)) {
echo "<font color='red'>Maximum Tents field is empty.</font><br/>";
}
if(empty($maxpersons)) {
echo "<font color='red'>Maximum Persons field is empty.</font>
<br/>";
}
if(empty($geography)) {
echo "<font color='red'>Geography field is empty.</font><br/>";
}
if(empty($view)) {
echo "<font color='red'>View field is empty.</font><br/>";
}
if(empty($forestcover)) {
echo "<font color='red'>Forest Cover field is empty.</font><br/>";
}
if(empty($waterfront)) {
echo "<font color='red'>Waterfront Access field is empty.</font>
<br/>";
}
if(empty($firepit)) {
echo "<font color='red'>Firepit field is empty.</font><br/>";
}
if(empty($description)) {
echo "<font color='red'>Description field is empty.</font><br/>";
}
if(empty($reslink)) {
echo "<font color='red'>Reservation Link Access field is empty.
</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($mysqli, "UPDATE sites SET
sitenumber='".$sitenumber."',videolink='".$videolink."'
,daynight='".$daynight."',
maxtents='".$maxtents."'
,maxpersons='".$maxpersons."'
,geography='".$geography."',
view='".$view."',forestcover='".$forestcover."',
waterfront='".$waterfront."',
firepit='".$firepit."',description='".$description."',reslink='".$reslink."'
WHERE sitenumber='".$sitenumber."'");
//redirectig to the display page. In our case, it is index.php
header("Location: index.php");
}
}
echo mysqli_error($mysqli);
?>
<?php
//getting id from url
$sitenumber = $_GET['site']; //Undefined index here
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE
sitenumber='".$sitenumber."'");
while($res = mysqli_fetch_array($result))
{
$sitenumber = $res['sitenumber'];
$videolink = $res['videolink'];
$daynight = $res['daynight'];
$maxtents = $res['maxtents'];
$maxpersons = $res['maxpersons'];
$geography = $res['geography'];
$view = $res['view'];
$forestcover = $res['forestcover'];
$waterfront = $res['waterfront'];
$firepit = $res['firepit'];
$description = $res['description'];
$reslink = $res['reslink'];
}
echo mysqli_error($mysqli);
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
Home
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Site Number</td>
<td><input type="number" name="sitenumber" value=<?php echo
$sitenumber;?>></td>
</tr>
<tr>
<td>YouTube Link</td>
<td><input type="url" name="videolink" value="<?php echo
$videolink;?>"></td>
</tr>
<tr>
<td>Day or Overnight</td>
<td><select name="daynight" value="<?php echo $daynight;?>">
<option value="Day">Day</option>
<option value="Overnight">Overnight</option></td>
</tr>
<tr>
<td>Maximum Tents</td>
<td><input type="number" name="maxtents" value="<?php echo
$maxtents;?>"></td>
</tr>
<tr>
<td>Maximum Persons</td>
<td><input type="number" name="maxpersons" value="<?php echo
$maxpersons;?>"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="text" name="geography" value="<?php echo
$geography;?>"></td>
</tr>
<tr>
<td>View</td>
<td><input type="text" name="view" value="<?php echo $view;?>">
</td>
</tr>
<tr>
<td>Forest Cover</td>
<td><input type="text" name="forestcover" value="<?php echo
$forestcover;?>"></td>
</tr
<tr>
<td>Waterfront Access</td>
<td><select name="waterfront" value="<?php echo $waterfront;?>">
<option value="Yes">Yes</option>
<option value="No">No</option></td>
</tr>
<tr>
<td>Firepit Availability</td>
<td><select name="firepit" value="<?php echo $firepit;?>">
<option value="Yes">Yes</option>
<option value="No">No</option></td>
</tr>
<tr>
<td>Site Description</td>
<td><input type="text" name="description" value="<?php echo
$description;?>"></td>
</tr>
<tr>
<td>Reservation Link</td>
<td><input type="url" name="reslink" value="<?php echo $reslink;?
>"></td>
</tr>
<td><input type="hidden" name="site" value="<?php echo
$_GET['site'];?>"></td> //Undefined here
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
So far you guys have been amazing with my other errors, I'm hoping you can point me in the right direction. And I know, I should use prepared statements, but I'm just learning how to do them now that I've been told what they are. I will fix that in due time. Right now, I just need to get this update page working.
Thanks.
What is $sitenumber = $_GET['site']; //Undefined index here supposed to be when it's not setup yet? What si the default value?
$sitenumber = $_GET['site'] ?? 'DEFAULT VALUE';
same as
$sitenumber = isset($_GET['site']) ? $_GET['site'] : 'DEFAULT VALUE';
Looks like you might want:
$sitenumber = $_GET['site'] ?? $sitenumber;
Since you define that var at the top.
The problem here is that GET['site'] can be unset at those positions.
You have a nice check to see if $_GET['site'] is set at the first part of the code, but the part where you are absolutely sure that $_GET['site'] is actually set ends just before the PHP end tag. After that $_GET['site'] is never checked again for if it exists.
There are two ways to solve this particular problem:
Either you set a default value to $sitenumber and use sitenumber at all places in you code where you need the $_GET['site']
Or you also put the second part inside an if statement, which means that you'd need to copy the current HTML and paste it without the $_GET['site'] and everything it depends on
I hope it is clear that you should just set a default value. The easiest way would be to use a null coalesce operator, which will mean that you'd need to make two small changes.
The first and most obvious: change $sitenumber = $_GET['site']; //Undefined index here to $sitenumber = $_GET['site'] ?? "your default value" ;
The second change is <td><input type="hidden" name="site" value="<?php echo
$_GET['site'];?>"></td> to <td><input type="hidden" name="site" value="<?php echo $sitenumber?>"></td>
I've a html form with multiple select field. I'm trying to validate it with php. but i can't validate this multiple select field with php. It's show me success message without any validation.
Please kindly tell me what's the problem in my code. Thank you.
Php Code:
<?php
if(isset($_POST['Submit']) && $_POST['Submit'] == "Send SMS")
{
if(isset($_POST['number']))
$number = $_POST['number'];
$msg = inputvalid($_POST['txt']);
$err = array();
if(isset($msg) && isset($number))
{
if(empty($msg) && empty($number))
$err[] = "All field require";
else
{
if(empty($msg))
$err[] = "Your message require";
if(empty($number))
$err[] = "Select your mobile number";
}
}
if(!empty($err))
{
echo "<div class='error'>";
foreach($err as $er)
{
echo "<font color=red>$er.</font><br/>";
}
echo "</div>";
echo "<br/>";
}
else
{
echo "good";
}
}
?>
Html Code:
<form name="frm" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<table width="800" border="0" cellspacing="10" cellpadding="0">
<tr>
<td valign="top">Number</td>
<td>
<select multiple="multiple" size="10" name="number[]">
<option value="">--Select Member--</option>
<?php
$class = mysql_query("SELECT * FROM e_members");
while($res = mysql_fetch_array($class))
{
$phone = $res['phone'];
?>
<option value="<?php echo $phone; ?>"> <?php echo $phone; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top">Write message</td>
<td>
<textarea class="textarea" placeholder="Your message" name="txt" onkeyup="counter(this);">
<?php if(isset($_POST['txt'])) echo $_POST['txt']; ?>
</textarea>
<br/>
<input type="" name="lbl" style="border:none;">
<br/>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit2" value="Save SMS" class="view"/>
<input type="submit" name="Submit" value="Send SMS" class="submit"/>
</td>
</tr>
</table>
</form>
Update:
After select multiple/single value var_dump showing:
array(1) { [0]=> string(13) "8801814758545" }
Without select it's showing:
NULL
You are trying to validate array $number using empty($number). It won't work as you expected
You can validate this as if (is_array($number) && count($number) > 0)
The problem is that your check for empty values is inside for check for if(isset($msg) && isset($number)) and as soon as you post the form these variables are set. As you are already check that the post is set then remove this outer if statement and just check for empty values and it should work.
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))