Undefined Index while retrieving variable from $_GET in URL - php

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>

Related

You have an error in your SQL syntax; - and Undefined Variable error - possibly connected

I'm pretty new to coding with php and SQL, so I'm probably going to have a lot of questions. But as the title states, I'm getting this error...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I'm not sure what this is referring to. I've gone over the code as much as I can, but I can't find a syntax error. Maybe it's something I just don't know yet.
<?php
// including the database connection file
include_once("config.php");
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'];
//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>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
Sorry for the long code here, but I felt it was a little necessary to see the full context here.
There is also a break somewhere with the variables. The sitenumber variable isn't updating, and every variable after that is getting this error...
Notice: Undefined variable: videolink in C:\wamp\www\code\edit.php on line 124
So, this is kind of a two pronged problem. Help would be greatly appreciated.
Correct this :
$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE sitenumber='".$sitenumber."' ");
And this :
$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'");
Your SQL query seems good, but the problem can come from the values of your variables.
Since your query is not escaped properly (and it should be for better security), I would advise you to debug your query before executing.
This way you will be able to understand what is going to be executed in your database.
If you don't use xdebug, you can just put your query into a variable and then dump it using var_dump.
Then, open phpmyadmin (I assume you have an access to it, at least), and paste the value of your variable (which is your query) into the SQL editor. Then execute it and you should have a message explaining where the error is.
It will help you understand why it is important to use prepared statement by seeing which variable has a wrong value (meaning it includes a ' or a ", for instance).
I hope it will help

Disable submit button if input field is empty

**Hello..i know my type of question has been answered in different questions before;but i tried them all..none worked!So please have a look on my issue.
I've a table that contains form input fields where values come from database.I didn't wanted the values to be edited.So used "readonly". But the problem is:By the inspect element of a browser when readonly is removed..then the value can be edited and blank input can be submitted !!! So i want to disable the editing or at least want to disable the submit button if input field is empty.**
The code of the table:
<?php
if (isset($_POST['show'])) {
$class = $_POST["Class"];
$sql = "SELECT * FROM students WHERE Class='$class' ORDER BY Roll ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
?>
<form action="" method="POST">
<table class="table table-bordered table-hover" style="width: 85%;text-align: center">
<tr >
<th>Roll</th>
<th>Student's Name</th>
<th>Attendance</th>
</tr>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><input value="<?php echo $row['Roll']; ?>" name="Roll[]" readonly required=""/></td>
<td><input value="<?php echo $row['Name']; ?>" name="Name[]" readonly required=""/></td>
<td><select name="Status[]">
<option value="0">0</option>
<option value="1">1</option>
</select></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="save" value="Save" style="width: 50%;margin-left: 20%">
</form>
<?php
} else {
$message = "Sorry! No result!";
echo "<script type='text/javascript'>alert('$message');</script>";
}
$conn->close();
}
?>
The insertion code:
<?PHP
if (isset($_POST["save"])) {
foreach ($_POST["Roll"] as $rec => $value) {
$Roll = $_POST["Roll"][$rec];
$Name = $_POST["Name"][$rec];
$Status = $_POST["Status"][$rec];
$Date = date('Y-m-d');
$sql = "INSERT INTO `attendance`(`id`, `Date`, `Roll`, `Name`, `Status`) VALUES ('','$Date','$Roll','$Name','$Status')";
}
if ($conn->query($sql) === TRUE) {
$message = "Saved !";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
this is correct way to not input empty field
$class = $_POST["Class"];
if(!empty($class)) {
$sql = "SELECT * FROM students WHERE Class='$class' ORDER BY DESC or ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
}
}
?>
I edited part of your code to disallow editing. I hope it serves your pourpose. i used disabled attribute on the input tags.
<tr>
<td><input value="<?php echo $row['Roll']; ?>" name="Roll[]" disabled/></td>
<td><input value="<?php echo $row['Name']; ?>" name="Name[]" disabled/></td>
<td><select name="Status[]">
<option value="0">0</option>
<option value="1">1</option>
</select></td>
</tr>

php - stop php if empty textbox and process other html forms

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.

Validating between 2 drop down menus so only one can be selected php

I have a script below which the user logs a php form.
There is a section with 2 drop down menus next to each other, 'npc_battery_n_code' and 'npc_battery_other_code'.
What I am trying to do is to make it so one of these drop down menus has to have an option selected for the form to be submitted, but not both or none.
So for example, if an option gets selected in 'npc_battery_n_code' and then the user selects another option in 'npc_battery_other_code', then the value of the 'npc_battery_n_code' will revert to the 'None' option.
Any suggestions welcome
<?php //logsale.php
require_once ('./includes/config.inc.php');
$page_title = 'Log a sale';
include ('./includes/header.html');
if (!isset($_SESSION['sp_user_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/login.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}
$users = $_SESSION['sp_user_id'];
?>
<h1>Heading</h1>
<?php
if (isset($_POST['submitted'])) {// Handle the form.
require_once ('mydatabase.php');
if (!empty($_POST['npc_quantity'])) {
$quantity = escape_data($_POST ['npc_quantity']);
} else {
$quantity = FALSE;
echo '<p><font color="red">You forgot to enter the Quantity</font></p>';
}
if (!empty($_POST['npc_customer_name'])) {
$customer = escape_data($_POST ['npc_customer_name']);
} else {
$customer = FALSE;
echo '<p><font color="red">You forgot to enter the Customer Name</font></p>';
}
if (!empty($_POST['npc_registration_no'])) {
$rego = escape_data($_POST ['npc_registration_no']);
} else {
$rego = FALSE;
echo '<p><font color="red">You forgot to enter the Customer Rego Number</font></p>';
}
if (!empty($_POST['npc_suo_no'])) {
$suo = escape_data($_POST ['npc_suo_no']);
} else {
$suo = FALSE;
echo '<p><font color="red">You forgot to enter the SUO Number</font></p>';
}
if (!empty($_POST['npc_amb_club_no'])) {
$repair_order = escape_data($_POST ['npc_amb_club_no']);
} else {
$repair_order = FALSE;
echo '<p><font color="red">You forgot to enter the A-Club number</font></p>';
}
if (!empty($_POST['npc_repair_order'])) {
$amb_club = escape_data($_POST ['npc_repair_order']);
} else {
$amb_club = FALSE;
echo '<p><font color="red">You forgot to enter the Repair Order</font></p>';
}
if (!empty($_POST['npc_invoice_no'])) {
$inv = escape_data($_POST ['npc_invoice_no']);
} else {
$inv = FALSE;
echo '<p><font color="red">You forgot to enter the Invoice Number </font></p>';
}
if (!empty($_POST['npc_entry_userdate'])) {
$inv_date = escape_data($_POST ['npc_entry_userdate']);
} else {
$inv_date = FALSE;
echo '<p><font color="red">You forgot to enter the Invoice Date</font></p>';
}
if ($quantity && $customer && $rego && $suo && $repair_order && $amb_club && $inv && $inv_date) {
$uid = #mysql_insert_id(); //Get the url ID.
$query = "INSERT INTO npc_sales_list
(npc_item_id , sp_user_id, npc_battery_n_code, npc_battery_other_code, npc_quantity,
npc_customer_name, npc_registration_no, npc_suo_no, npc_amb_club_no, npc_repair_order,
npc_entry_userdate, npc_usertype, npc_points, npc_bonus_points, npc_entry_date)
VALUES
('$uid', '$users', '$_POST[npc_battery_n_code]', '$_POST[npc_battery_other_code]', '$quantity',
$customer , $rego, $suo, $amb_club , $repair_order,
$inv , 'NPC', '5' , '0' , NOW())";
$result = mysql_query ($query) or trigger_error
("Query: $query\n<br />MySQL Error: " .mysql_error());
if ($result = #mysql_query($query)) {
//Finish the page.
echo '<p>The sale with invoice number: <strong>' . $inv . '</strong> <br />
is now registered into the system.</p>
<p>If you would like to log in further sales, please click here</p>
<p>If you would like to view your sales log, please click here</p>
';
include ('./includes/footer.html'); // Include the HTML footer.
exit();
} else { // If it did not run OK.
echo '<p><font color="red" size="+1">You could not be
registered due to a system error. We apologize for any
inconvenience.</font></p>';
}
}
} else { // If one of the data tests failed.
echo '<p><font color="red" size="+1">Please check all manatory fields are complete and try again.
</font></p>';
}
mysql_close(); // Close the database connection.
?>
<form enctype="multipart/form-data" action="logsale.php" method="post">
<table width="520" border="0" cellspacing="3" cellpadding="2">
<tr>
<td width="184"><div align="right">NPC Vehicle<span class="style2">*</span></div></td>
<td width="323"><select name="npc_battery_n_code" id="npc_battery_n_code">
<option value="None"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == 'None')
{echo 'selected="selected"';} ?>>None
</option>
<option value="55D23L"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23L')
{echo 'selected="selected"';} ?>>55D23L
</option>
<option value="55D23R"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23R')
{echo 'selected="selected"';} ?>>55D23R
</option>
<option value="75D23R"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '75D23R')
{echo 'selected="selected"';} ?>>75D23R
</option>
</select></td>
</tr>
<tr>
<td width="184"><div align="right">Other <span class="style2">*</span></div></td>
<td width="323">
<select name="npc_battery_other_code" id="npc_battery_other_code">
<option value="50D20L"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '50D20L')
{echo 'selected="selected"';} ?>>50D20L
</option>
<option value="50D20R"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '50D20R')
{echo 'selected="selected"';} ?>>50D20R
</option>
<option value="55D23LMF"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '55D23LMF')
{echo 'selected="selected"';} ?>>55D23LMF
</option>
<option value="55D23RMF"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '55D23RMF')
{echo 'selected="selected"';} ?>>55D23RMF
</option>
</select></td>
</tr>
<tr>
<td><div align="right">Quantity <span class="style2">*</span></div></td>
<td><input type="text" name="npc_quantity" size="10" maxlength="10"
value="<?php if (isset($_POST['npc_quantity'])) echo $_POST['npc_quantity']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Customer name<span class="style2">*</span></div></td>
<td><input type="text" name="npc_customer_name" size="30" maxlength="30"
value="<?php if (isset($_POST['npc_customer_name'])) echo $_POST['npc_customer_name']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Rego number <span class="style2">*</span></div></td>
<td><input type="text" name="npc_registration_no" size="10" maxlength="7"
value="<?php if (isset($_POST['npc_registration_no'])) echo $_POST['npc_registration_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">SUO No.<span class="style2">*</span></div></td>
<td><input type="text" name="npc_suo_no" size="10" maxlength="7"
value="<?php if (isset($_POST['npc_suo_no'])) echo $_POST['npc_suo_no']; ?>"/></td>
</tr>
<tr>
<td><div align="right">A-Club ID<span class="style2">*</span></div></td>
<td><input type="text" name="npc_amb_club_no" size="15" maxlength="7"
value="<?php if (isset($_POST['npc_amb_club_no'])) echo $_POST['npc_amb_club_no']; ?>"/></td>
</tr>
<tr>
<td><div align="right">Repair Order <span class="style2">*</span></div></td>
<td><input type="text" name="npc_repair_order" size="15" maxlength="12"
value="<?php if (isset($_POST['npc_repair_order'])) echo $_POST['npc_repair_order']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Invoice No. <span class="style2">*</span></div></td>
<td><input size="10" name="npc_invoice_no"
value="<?php if (isset($_POST['npc_invoice_no'])) echo $_POST['npc_invoice_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Invoice Date <span class="style2">*</span></div></td>
<td><input size="12" name="npc_entry_userdate" maxlength="10"
value="<?php if (isset($_POST['npc_entry_userdate'])) echo $_POST['npc_entry_userdate']; ?>"/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></p><input type="hidden" name="submitted" value="TRUE" />
</td>
</tr>
</table>
</form>
<p>Footer</p>
<?php
include ('./includes/footer.html');
?>
jquery: select and unselect items of listbox is what you are looking for.
How you want your script to work cannot be achieved with PHP or Mysql. You can only work with the data when the website is loaded. If you want to edit the data without refreshing the page you need to use JavaScript, or more advanced jQuery.

PHP Form checkbox question

I have a form that takes the following inputs:
Name: IBM
Surface(in m^2): 9
Floor: (Checkbox1)
Phone: (Checkbox2)
Network: (Checkbox3)
Button to send to a next php page.
All those values above are represented in a table when i press the submit button.
The first two (name and surname) are properly displayed in the table.
The problem is with the checkboxes. If i select the first checkbox the value in the table should be presented with 1. If its not selected the value in the table should be empty.
echo "<td>$Name</td>"; // works properly
echo "<td>$Surface</td>"; // works properly
echo "<td>....no idea for the checkboxes</td>;
Some part of my php code with the variables:
<?php
if (!empty($_POST))
{
$name= $_POST["name"];
$surface= $_POST["surface"];
$floor= $_POST["floor"];
$phone= $_POST["telefoon"];
$network= $_POST["netwerk"];
if (is_numeric($surface))
{
$_SESSION["name"]=$name;
$_SESSION["surface"]=$surface;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($floor) && ($phone) && ($network))
{
$_SESSION["floor"]=$floor;
$_SESSION["phone"]=$phone;
$_SESSION["network"]=$network;
header("Location:ExpoOverzicht.php");
}
}
?>
Second page with table:
<?php
$name= $_SESSION["name"];
$surface= $_SESSION["surface"];
$floor= $_SESSION["floor"];
$phone= $_SESSION["phone"];
$network= $_SESSION["network"];
echo "<table class=\"tableExpo\">";
echo "<th>name</th>";
echo "<th>surface</th>";
echo "<th>floor</th>";
echo "<th>phone</th>";
echo "<th>network</th>";
echo "<th>total price</th>";
for($i=0; $i <= $_SESSION["name"]; $i++)
{
echo "<tr>";
echo "<td>$name</td>"; // gives right output
echo "<td>$surface</td>"; // gives right output
echo "<td>...</td>"; //wrong output (ment for checkbox 1)
echo "<td>...</td>"; //wrong output (ment for checkbox 2)
echo "<td>...</td>"; //wrong output (ment for checkbox 3)
echo "<td>....</td>";
echo "</tr>;";
}
echo "</table>";
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="18"/></td>
</tr>
<tr>
<td>Surface(in m^2):</td>
<td><input type="text" name="surface" size="6"/></td>
</tr>
<tr>
<td>Floor:</td>
<td><input type="checkbox" name="floor" value="floor"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="checkbox" name="phone" value="phone"/></td>
</tr>
<tr>
<td>Network:</td>
<td><input type="checkbox" name="network" value="network"/></td>
</tr>
<tr>
<td><input type="submit" name="verzenden" value="Verzenden"/></td>
</tr>
</table>
There might be a few spelling mistakes since i had to translate it.
Best regards.
Instead of directly assigning your checkbox variables, see if they have been checked or not first.
$verdieping = isset($_POST["floor"]) ? $_POST["floor"] : 0;
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
This way, if the user hasn't ticked a checkbox, you have a value of '0' assigned to it instead of an undefined variable.
If you declare a checkbox with:
<input type="checkbox" name="mycheckbox" value="1">
you can check the value after submitting the form by:
if(!empty($_POST["mycheckbox"])) {
// checkbox was checked
}
else {
// checkbox was not checked
}
In this php page you can write like this, it may be solution of your question
if (!empty($_POST))
{
$standnaam = $_POST["name"];
$oppervlakte = $_POST["surface"];
$verdieping = $_POST["floor"];
$telefoon = $_POST["telefoon"];
$netwerk = $_POST["netwerk"];
if (is_numeric($oppervlakte))
{
$_SESSION["name"]=$standnaam;
$_SESSION["surface"]=$oppervlakte;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($verdieping) && ($telefoon) && ($netwerk))
{
$_SESSION["floor"]=$verdieping;
$_SESSION["phone"]=$telefoon;
$_SESSION["network"]=$netwerk;
header("Location:ExpoOverzicht.php");
}
}

Categories