MVC function is not giving a result - php

Recently im trying to do a model view controller for my software engneering course project. My device model functions are all working fine except for the function listallmydevices() idk whats going on and no errors are visible.
Note i've changed my function to another list function from another class and its working fine except for the device
<html>
<head>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
</head>
<style>
.dropdown-submenu
{
position: relative;
}
.dropdown-submenu .dropdown-menu
{
top: 0;
left: 100%;
margin-top: -1px;
}
</style>
<form action="../prototype/mvc/DeviceController.php" method="post" enctype="multipart/form-data">
<label for="id">ID:</label>
<input type="text" name="ID" >
<br><br>
<label for="username">Device Name:</label>
<input type="text" id="un" name="Name">
<br><br>
<label for="lastname">Date:</label>
<input type="text" name="Date">
<br><br>
<label for="fs">Amount:</label>
<input type="text" name="Amount">
<br><br>
<label for="ls">Price:</label>
<input type="text" name="Price">
<br><br>
<label for="mail">Brand:</label>
<input type="mail" name="Brand">
<br><br>
<label for="password">MarketingToolsID:</label>
<input type="password" name="MTID">
<br><br>
<label for="password">Tochange: </label>
<input type="password" name="edit">
<br><br>
<label for="password">Tochange: </label>
<select name="Cateogry">
<option>Price </option>
<option>Amount </option>
<option> Name</option>
<option> </option>
<option> </option>
</select>
<br>
<input type="submit" name="DELETEDEVICE" value="DELETE DEVICE">
<input type="submit" name="ADDDEVICE" value="ADD DEVICE" >
<input type="submit" name="VIEWALLDEVICES" value="VIEW ALL DEVICES" >
<input type="submit" name="EDITDEVICE" value="Edit DEVICES" >
</form>
-->
public static function listalldevices()
{
$conn = mysqli_connect("localhost", "root", "", "bioster");
$sql="Select * from device order by DeviceID";
$devicequery= mysqli_query($conn,$sql) or die(mysqli_error($conn));
$i=0;
$result;
while($row= mysqli_fetch_array($devicequery))
{
$devobj=new device($row["DeviceID"]);
$result[$i]=$devobj;
$i++;
}
return $result;
}
Device controller
if(isset($_POST['VIEWALLDEVICES']))
{
$array= Device::listalldevices();
echo "All devices";
for($i=0; $i<count($array); $i++)
{
echo $array[$i]->DeviceID;
echo $array[$i]->Name;
echo $array[$i]->Date;
echo $array[$i]->Amount;
echo $array[$i]->Price;
echo $array[$i]->Brand;
}
} //check this not wotking

Related

Having trouble posting information from a html form to php script. (Undefined index)

I'm working on an app for PHP class where we have to create an app to order pizza online. I'm stuck on an html form that is not posting on the script and I can't get it to work. I'm getting the following error and I don't know how to fix it.
I'm working on an app for PHP class where we have to create an app to order pizza online. I'm stuck on an html form that is not posting on the script and I can't get it to work. I'm getting the following error and I don't know how to fix it. Undefined index error
HTML index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>HTech Pizza Online Order</title>
</head>
<body>
<div class="container">
<form action="receipt.php" method="POST">
<h2>HTech Pizzeria Online Order</h2>
<fieldset>
<legend><b>Customer Information</b></legend>
Name: <input type="text" require id="customer" name="customer"
placeholder="Enter Your Name..."><br>
Phone Number: <input type="text" require id="phoneNumber" name="phoneNumber"
placeholder="Enter Your Phone Number..."><br>
Address: <input type="text" require id="address" name="address"
placeholder="Enter Your Address..."><br>
</fieldset>
</form>
</div>
<form action="receipt.php" method="POST">
<fieldset>
<legend><b>Order Customization</b></legend>
<div class="checkbox">
<b>Size : </b><br><br>
<input id="radio3" type="radio" name="size" value="Large"><label for="radio3"><span><span></span></span>Large: $10.00</label> <br>
<input id="radio2" type="radio" name="size" value="Medium"><label for="radio2"><span><span></span></span>Medium: $8.00</label> <br>
<input id="radio1" type="radio" name="size" value="Small" checked="checked"><label for="radio1"><span><span></span></span>Personal: $6.00</label>
<br>
<br>
<b>Crust : </b><br><br>
<input id="radio4" type="radio" name="crust" value="traditional" checked="checked"><label for="radio4"><span><span></span></span>Traditional: $2.00</label><br>
<input id="radio5" type="radio" name="crust" value="welldone"><label for="radio5"><span><span></span></span>Well Done: $2.00</label><br>
<input id="radio6" type="radio" name="crust" value="thin"><label for="radio6"><span><span></span></span>Thin: $2.00</label><br><br>
<hr>
</div>
<div class="checkbox"><b>Toppings:</b><br><br>
<div>
<input id="toppingBox1" type="checkbox" name="toppings[]" value="Pepperoni"><label for="toppingBox1"><span></span>Pepperoni</label>
</div>
<div>
<input id="toppingBox2" type="checkbox" name="toppings[]" value="Bacon"><label for="toppingBox2"><span></span>Bacon</label>
</div>
<div>
<input id="toppingBox3" type="checkbox" name="toppings[]" value="Chicken"><label for="toppingBox3"><span></span>Chicken</label>
</div>
<div>
<input id="toppingBox4" type="checkbox" name="toppings[]" value="Sausage"><label for="toppingBox4"><span></span>Sausage</label>
</div>
<div>
<input id="toppingBox5" type="checkbox" name="toppings[]" value="Ham"><label for="toppingBox5"><span></span>Ham</label>
</div>
<div>
<input id="toppingBox6" type="checkbox" name="toppings[]" value="BlackOlives"><label for="toppingBox6"><span></span>Black Olives</label>
</div>
<div>
<input id="toppingBox7" type="checkbox" name="toppings[]" value="Parmesan"><label for="toppingBox7"><span></span>Parmesan</label>
</div>
<div>
<input id="toppingBox8" type="checkbox" name="toppings[]" value="Pineapple"><label for="toppingBox8"><span></span>Pineapple</label>
</div>
</div>
<hr>
<div class="checkbox"><b>Student Discount:
</b><br><br>
<input id="radio7" type="radio" name="studentBol" value="student"><label for="radio7"><span><span></span></span> Student</label> <br>
<input id="radio8" type="radio" name="studentBol" value="nonStudent"><label for="radio8"><span><span></span></span>Non - Student</label><br>
<br><hr><br>
<b>Delivery or Pickup:</b> <br><br>
<input id="radio9" type="radio" name="modePickup" value="Delivery"><label for="radio9"><span><span></span>
</span>Delivery</label><br>
<input id="radio0" type="radio" name="modePickup" value="Pickup"><label for="radio0"><span><span></span></span>Store Pickup</label><br>
<br>
<b>Boxes Of Pizza:</b><br><br>
<div class="custom-select">
<label>
<select name="noOfPizza">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</label>
</div>
</div>
<br>
<br>
Additional Instructions:<br><br>
<label>
<textarea name="addInstructions" cols="60" rows="10"></textarea>
</label>
<br>
<br>
<input class="btn btn1" type="submit" name="order" value="Order Now">
</fieldset>
<br>
</form>
</body>
</html>
PHP Script
<?php
if (isset($_POST['order'])) {
// Check for pizza size
switch ($_POST['size']) {
case "Small":
$pizzaSizePrice = 6.00;
break;
case "Medium":
$pizzaSizePrice = 8.00;
break;
case "Large":
$pizzaSizePrice = 10.00;
break;
}
// Checking Student Qualification
if ($_POST['studentBol'] == "student") {
$studentBol = true;
} else {
$studentBol = false;
}
// Toppings
$toppingArray = $_POST['toppings'];
if (count($toppingArray) == 1) {
$toppingAmount = 0.00;
} else {
// First topping free, added toppings an extra $1
$toppingAmount = count($toppingArray) - 1;
}
// Pickup
if ($_POST['modePickup'] == "Delivery") {
$modePickup = 5.00;
} else {
$modePickup = 0.00;
}
$crustType = 2.00;
$numberOfPizza = $_POST['noOfPizza'];
$instructions = $_POST['addInstructions'];
// Calculating Amount
$netAmount = $crustType + $pizzaSizePrice + $toppingAmount;
$netAmount = $netAmount * $numberOfPizza;
// Adding Delivery Fee
$netAmount = $netAmount + $modePickup;
if ($studentBol) {
$studentDiscount = $netAmount * 10 / 100;
// Applying the student discount
$netAmount = $netAmount - $studentDiscount;
$studentDiscount = number_format($studentDiscount, 2, '.', '');
}
// adding tax
$salesTax = $netAmount * 10 / 100;
$salesTax = number_format($salesTax, 2, '.', '');
$total = $salesTax + $netAmount;
$total = number_format($total, 2, '.', '');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Order Confirmation</title>
</head>
<body>
<h2>Order Confirmation</h2>
<hr>
<h3>Customer Information:</h3>
<h3>First Name:</h3><?php echo $_POST['customer']; ?>
<h3>Phone Number:</h3><?php echo $_POST['phoneNumber']; ?>
<h3>Address:</h3><?php echo $_POST['address']; ?>
<hr>
<h3>Size: </h3><?php echo $_POST['size'] . " $" . $pizzaSizePrice; ?>
<h3>Crust Type: </h3><?php echo $_POST['crust'] . " $2"; ?>
<h3>Toppings: </h3>
<?php
if (count($toppingArray) == 1) {
foreach ($toppingArray as $topping) {
echo $topping . " Free<br>";
}
} else {
foreach ($toppingArray as $topping) {
// First topping free
if ($topping == "Peperroni") {
echo $topping . " Free<br>";
} else {
echo $topping . " $1<br>";
}
}
}
?>
<h3>Pizza's Ordered: </h3><?php echo $numberOfPizza; ?>
<h3>Additional Instructions:</h3> <?php echo $_POST['addInstructions']; ?>
<br>
<hr>
<h3>Delivery or Pickup: </h3><?php echo $_POST['modePickup'] . " $" . $modePickup; ?>
<!-- Check Student Discount -->
<?php
if ($studentBol) {
echo "<br><br>Student Discount: $" . $studentDiscount;
}
?>
<h3>Sales Tax:</h3> $<?php echo $salesTax; ?>
<hr>
<h3>Total Due on Delivery: </h3> $<?php echo $total; ?>
</body>
</html>
It's because you have two forms in your HTML.
Refer to this part of your code:
...
<form action="receipt.php" method="POST">
<h2>HTech Pizzeria Online Order</h2>
<fieldset>
<legend><b>Customer Information</b></legend>
Name: <input type="text" require id="customer" name="customer"
placeholder="Enter Your Name..."><br>
Phone Number: <input type="text" require id="phoneNumber" name="phoneNumber"
placeholder="Enter Your Phone Number..."><br>
Address: <input type="text" require id="address" name="address"
placeholder="Enter Your Address..."><br>
</fieldset>
</div>
<form action="receipt.php" method="POST">
...
You're only submitting the second of the two forms and the customer info is in the first form. Try consolidating your fields into one form and you should be good to go.
Hope this helps.

how to change the entered text font family value dynamically?

I have a very simple doubt, I have a input text and a drop down list and in that drop down list their are various font family, so if I enter any text in the input field and later if I select the font family from the dropdown list as arial then the entered text should be displayed in arial font. Please can any one guide me I'm very much close to completing the task.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body style="padding-left:200px">
<?php
$name = $font = $size = "" ;
if (isset($_POST["font"])) {
$name = test_input($_POST["name"]);
$font = test_input($_POST["font"]);
var_dump($font);
$size = test_input($_POST["size"]);
var_dump($size);
}
function test_input($data) {
return $data;
}
?>
<form action="" method="post">
<br><br>
<div>
<label style="color:orange">Enter Text: </label> <input type="text" name="name" placeholder="Enter Something">
</div><br><br>
<div>
<label style="color:orange">Select Size: </label> <input type="text" name="size">
</div><br><br>
<div>
<label class="control-label" style="color:orange;">Font Name :</label>
<select name="font" style="margin-left:14px; color:black;">
<option disabled selected value> -- select an option -- </option>
<option>Times New Roman</option>
<option>Arial</option>
<option>Verdana</option>
<option>Georgia</option>
<option>Impact</option>
</select>
</div><br><br>
<!--div>
<label style="color:orange">Choose Color: </label> <input type="text" name="size">
</div><br><br-->
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<br>";
echo "<h2 style='color:orange'>OUTPUT:</h2>";
echo '<p style="font-family:'.$font.'; font-size:'.$size.';">'.$name.'</p>';
?>
</body>
</html>
Try this. Just echo your font variable in the variable you are echoing.
Put the value returning font in a variable and use that variable while you echo.
if (isset($_POST["font"])) {
$name = test_input($_POST["name"]);
$font = test_input($_POST["font"]);
}
<!DOCTYPE html>
<html>
<head>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body style="padding-left:200px">
<?php
$name = "";
$font='arial';
$size='14px';
if (isset($_POST["font"])) {
$name = test_input($_POST["name"]);
$font = test_input($_POST["font"]);
$size = test_input($_POST["size"]);
}
function test_input($data) {
return $data;
}
?>
<form action="" method="post">
<br><br>
<div>
<label style="color:orange">Enter Text: </label> <input type="text" name="name" placeholder="Enter Something">
</div><br><br>
<div>
<label class="control-label" style="color:orange;">Font Name :</label>
<select name="font" style="margin-left:14px; color:black;">
<option disabled selected value> -- select an option -- </option>
<option>Times New Roman</option>
<option>Arial</option>
<option>Verdana</option>
<option>Georgia</option>
<option>Impact</option>
</select>
</div><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<br>";
echo "<h2 style='color:orange'>OUTPUT:</h2>";
echo '<p style="font-family:'.$font.';font-size:'.$size.';">'.$name.'</p>';
?>
</body>
</html>

Uploading file to MySQL blob field file_get_contents(): failed (from input type="file")

I am uploading a file from my local directory to a MySQL Databases' MEDIUMBLOB field using file_get_contents and I am receiving the following error:
Warning: file_get_contents(test.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\Craisins\forms\new_scenario.php on line 265
Yet the file is definitely there since I pick it with input type="file"
This seemed to be working yesterday and now suddenly today it's no longer working correctly and I'm not sure why.
Here is the full file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<!--Normal CSS Sheets-->
<link rel="stylesheet" type="text/css" href="../includes/style.css">
<link rel="stylesheet" type="text/css" href="../includes/style_form.css">
<!--BSM Select CSS Sheet-->
<link rel="stylesheet" type="text/css" href="../includes/bsmselect/css/jquery.bsmselect.css">
<!--jQuery-->
<script src="../includes/jquery-2.1.4.min.js"></script>
<!--jQuery for BSMSelect-->
<script src="../includes/bsmselect/js/jquery.bsmselect.js"></script>
<!--jQuery selector for BSMSelect-->
<script>
$(document).ready(function() {
$("select[multiple]").bsmSelect();
});
</script>
<style type="text/css">
<!--
.style1
{
font-size: 12px;
font-weight: bold;
}
textarea
{
resize: none;
}
-->
<?php include ("../includes/formSuccess.php");
include ("../includes/sql.php");
require_once "../WindowsAzure/WindowsAzure.php";
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Common\ServiceException;
?>
</style>
<title>****</title>
</head>
<body>
<div id="pageContainer">
<!--This section is for the upper half of the page. From the "Title" bar up.-->
<?php include ("../includes/formNav.php");?>
<!--End of the Header Section-->
<br>
<!--This section is where we add the main content for the page.-->
<div id="contentArea">
<div id="mainContentAdmin">
<h1 style="text-align:center">Add New Scenario</h1>
<?php
// display form if user has not clicked submit
if (!isset($_POST["btn_submit"]))
{
?>
<!--This will be the form that will hold the information of the entire page.-->
<form class="elegant-aero" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<!--Scenario Title-->
<label>
<span>Title:</span>
<input type="text" name="title" placeholder="Enter the full scenario name here">
</label>
<!--Scenario Description-->
<label>
<span>Description:</span>
<textarea name="desc" placeholder="Enter the scenario description here"></textarea>
</label>
<!--Scenario Times-->
<label id="time">
<span>Estimated Times:</span>
<!--Scenario Execution Time-->
<b>Execution:</b>
<input type="number" name="execHr" min="0" placeholder="Hours">
<input type="number" name="execMin" min="1" placeholder="Minutes">
<br><br>
<!--Scenario Debriefing Time-->
<b>Debriefing:</b>
<input type="number" name="debriHr" min="0" placeholder="Hours">
<input type="number" name="debriMin" min="0" placeholder="Minutes">
</label>
<br>
<!--Origin-->
<label>
<span>Origin:</span>
<input type="text" name="origin" placeholder="Enter the origin here (if any)">
</label>
<!--Target Users-->
<label>
<span>Target Users:</span>
<select name="users[]" multiple="multiple" title="Please select user type...">
<?php
selectMySQL("valid_trainee_type","trainee_type");
?>
</select>
</label>
<br>
<!--Capabilities-->
<label>
<span style="">Capabilities Required:</span>
<select name="capabilities[]" multiple="multiple" title="Please select capabilities...">
<?php
selectMySQL("valid_capability","name");
?>
</select>
</label>
<br>
<!--Prerequisites-->
<label>
<span style="">Prerequisite Knowledge:</span>
<select name="prerequisites[]" multiple="multiple" title="Please select prerequisites...">
<?php
selectMySQL("valid_prerequisite","prerequisite");
?>
</select>
</label>
<br>
<!--Equipment-->
<label>
<span>Equipment Required:</span>
<select name="equipment[]" multiple="multiple" title="Please select equipment...">
<?php
selectMySQL("valid_equipment","equipment");
?>
</select>
</label>
<!--Parameters-->
<label>
<span>Parameters to Monitor:</span>
<select name="parameters[]" multiple="multiple" title="Please select parameters...">
<?php
selectMySQL("valid_parameter","parameter");
?>
</select>
</label>
<br>
<!--Learning Objectives-->
<label>
<span>Learning Objectives:</span>
<textarea name="object" placeholder="Please list the learning objectives for the procedure..."></textarea>
</label>
<!--Setting-->
<label>
<span>Setting:</span>
<textarea name="set" placeholder="Please describe the ideal setting for the procedure..."></textarea>
</label>
<!--Preparation-->
<label>
<span>Preparation Required:</span>
<textarea name="prep" placeholder="Please list any steps that are required in order to perform this procedure..."></textarea>
</label>
<!--Participants-->
<label>
<span>Participants Required:</span>
<textarea name="part" placeholder="Please list the participants that will be required in order to perform this procedure..."></textarea>
</label>
<!--Patient Information-->
<label>
<span id="patient">Patient Information:</span>
<br>
<!--Patient Name-->
<b>Name</b>
<input type="text" name="patName" style="width:63%;" placeholder="Enter the patient's full name here">
<!--Patient Vitals-->
<b>Vitals</b>
<select name="patSex" style="width:15%;" title="Male or Female?">
<option value="male">male</option>
<option value="female">female</option>
</select>
<b>Age</b>
<input name="patAge" type="text" style="width:5%;">
<b>Height</b>
<input type="text" name="patHt" style="width:6%;" placeholder="(in)">
<b>Weight</b>
<input type="text" name="patWt" style="width:6%;" placeholder="(lbs)">
<span style="width: 30%; border-right:0; margin-right:0; padding-right: 5px;">Other Info</span>
<textarea style="width:63%;" name="patInfo" placeholder="Any other medical details?"></textarea>
</label>
<!--Scoring-->
<label>
<span>Scoring:</span>
<textarea name="score" placeholder="Please detail the scoring rubric for this scenario..."></textarea>
</label>
<!--Debrief-->
<label>
<span>Debrief:</span>
<textarea name="debrief" placeholder="Please detail the debriefing process for this scenario..."></textarea>
</label>
<!--Scenario File-->
<label>
<span>Scenario Details:</span>
<br>
<input type="file" name="fileScenDetails" value="Upload File">
<br>
<textarea name="textScenDetails" placeholder="Please any other scenario details here..."></textarea>
</label>
<!--Submit Button-->
<label>
<span> </span>
<input type="submit" name="btn_submit" class="button" value="Add Scenario"/>
</label>
</p>
</form>
<?php
} //end if
else
{
//Setup the MySQL server
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "****";
//Create connection to the MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if($conn -> connect_error)
{
die("Connection failed: " . $conn -> connect_error);
} //end if
//Organize Execution Time
$exec_time = "00:".$_POST['execHr'].":".$_POST['execMin'];
//Organize Debriefing Time
$debrif_time = "00:".$_POST['debriHr'].":".$_POST['debriMin'];
//Organize BLOB
$blob = addslashes(file_get_contents($_POST['fileScenDetails']));
$sql="INSERT INTO scenario (title, description, est_scenario_time, est_debriefing_time,
origin, objectives, setting, preparation, participants,
patient_name, patient_sex, patient_age, patient_height,
patient_weight, patient_info, scoring, debrief,
scenario_file, scenario_text)
VALUES ('".$_POST['title']."','".$_POST['desc']."', '$exec_time', '$debrif_time',
'".$_POST['origin']."','".$_POST['object']."',
'".$_POST['set']."','".$_POST['prep']."',
'".$_POST['part']."','".$_POST['patName']."',
'".$_POST['patSex']."','".$_POST['patAge']."',
'".$_POST['patHt']."','".$_POST['patWt']."',
'".$_POST['patInfo']."','".$_POST['score']."',
'".$_POST['debrief']."','$blob',
'".$_POST['textScenDetails']."');";
if($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
} //end else
$conn->close();
/*
//Insert Target Users (Trainee Types)
insert_Scen_Dev('scenario','title', 'title','users',
'valid_trainee_type','trainee_type', 'scenario_trainee_type',
'scenario_id', 'valid_trainee_type_id');
//Insert Capabilities
insert_Scen_Dev('scenario','title', 'title','capabilities',
'valid_capability','name', 'scenario_capabilities',
'scenario_id', 'valid_capability_id');
//Insert Prerequisites
insert_Scen_Dev('scenario','title', 'title','prerequisites',
'valid_prerequisite','prerequisite', 'scenario_prerequisites',
'scenario_id', 'valid_prerequisite_id');
//Insert Equipment
insert_Scen_Dev('scenario','title', 'title','equipment',
'valid_equipment','equipment', 'scenario_equipment',
'scenario_id', 'valid_equipment_id');
//Insert Parameters
insert_Scen_Dev('scenario','title', 'title','parameters',
'valid_parameter','parameter', 'scenario_parameters',
'scenario_id', 'valid_parameter_id');
*/
echo "<div class='sqlSuccess'>A new Scenario has been added!</div>";
} //end else
?>
</div>
</div>
<!--End of the Content Section-->
<!--This section is for the lower half of the page. From the copyright bar down.-->
<div id="footer">
<div id="footerCenter">
Copyright ©
<script language="javascript" type="text/javascript">
var today = new Date();
var year = today.getFullYear();
document.write(year);
</script>
</div>
</div>
<!--End of the Footer Section-->
</div>
</body>
There are several problems with this code. At first, file won't upload unless you specify a correct enctype attribute on a form element:
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Also the file address is available under $_FILES['fileScenDetails']['tmp_name'], not under $_POST['fileScenDetails'], so:
$blob = addslashes(file_get_contents($_FILES['fileScenDetails']['tmp_name']));
Note, that you should check if file upload was successfull, as otherwise your code will trigger warning (eg. when user won't specify file). Add something like this:
if(isset($_FILES['fileScenDetails']['error']) && UPLOAD_ERR_OK == $_FILES['fileScenDetails']['error']) {
// do your stuff here
} else {
// display 'no-file' error to the user
}
Also, you should definitely check if the specified file was really uploaded, to avoid stealing your code with malformed requests. Check is_uploaded_file function here: http://php.net/manual/en/function.is-uploaded-file.php
There are also some possible SQL injections and other security issues.

Validating php within html tags

I have a registration page that uses both html and php I wish to validate the ALL user information before he recieves this alert >> Your ID number and password will send to your email address when the casher confirm your enrollment
Here is my code
<html>
<head>
<title>
</title>
<link rel="stylesheet" href="css/main.css" />
<style type="text/css">
<!--
.ed{
border-style:solid;
border-width:thin;
border-color:#00CCFF;
padding:5px;
margin-bottom: 4px;
}
#button1{
text-align:center;
font-family:Arial, Helvetica, sans-serif;
border-style:solid;
border-width:thin;
border-color:#00CCFF;
padding:5px;
background-color:#00CCFF;
height: 34px;
}
-->
</style>
<script type="text/javascript">
function validateForm()
{
{
alert("Your ID number and password will send to your email address when the casher confirm your enrollment");
}
}
</script>
</head>
<body>
<div id="mainwrapper">
<div id="header">
<img src="images/amuni.jpg">
</div>
<div id="menu">
<ul>
<li>
Home
</li>
<li>
Login
</li>
<li>
Student Pre Registration
</li>
<li>
About Us
</li>
<li>
Help
</li>
<div class="clearfix"></div>
</ul>
</div>
<div id="main" style="padding:20px; text-align:justify; font-family:arial;">
<form action="save_pre_reg.php" method="post" onsubmit="return validateForm()">
Firstname <br />
<input name="fname" type="text" class="ed" id="brnu" />
<br>
Lastname <br />
<input name="lname" type="text" class="ed" id="brnu" />
<br>
Middlename <br />
<input name="mname" type="text" class="ed" id="brnu" />
<br>
Gender<br />
<select name="gender" class="ed">
<option>Male</option>
<option>Female</option>
</select>
<br>
Email<br />
<input name="email" type="text" class="ed" id="brnu" />
<br>
Address <br />
<textarea name="address" class="ed"></textarea>
<br>
Age<br />
<input name="age" type="text" class="ed" id="brnu" />
<br>
Birthday<br />
<select name="month" class="ed">
<option>Month</option>
<?php
$N = 13;
for($i=1; $i < $N; $i++)
{
echo '<option>'.$i.'</option>';
}
?>
</select>
/
<select name="day" class="ed">
<option>Days</option>
<?php
$N = 32;
for($i=1; $i < $N; $i++)
{
echo '<option>'.$i.'</option>';
}
?>
</select>
/
<select name="year" class="ed">
<option>Year</option>
<?php
$N = 2020;
for($i=1889; $i < $N; $i++)
{
echo '<option>'.$i.'</option>';
}
?>
</select>
<br>
Level<br />
<select name="level" class="ed">
<option>Grade 1</option>
<option>Grade 2</option>
<option>Grade 3</option>
<option>Grade 4</option>
<option>Grade 5</option>
<option>Grade 6</option>
<option>1st year</option>
<option>2nd year</option>
<option>3rd year</option>
<option>4th year</option>
</select>
<br>
Payment Mode<br />
<select name="paymode" class="ed">
<option>Full Payment</option>
<option>Installment</option>
</select>
<br>
Family Income (monthly)<br />
<select name="fincome" class="ed">
<option>12,000 - 19,000</option>
<option>20,000 - 29,000</option>
<option>30,000 - 39,000</option>
<option>40,000 - 49,000</option>
<option>50,000 - 59,000</option>
<option>60,000 - 69,000</option>
<option>70,000 - 79,000</option>
<option>80,000 - 89,000</option>
<option>90,000 - 99,000</option>
<option>100,000 - above</option>
</select>
<br>
Mother's Name <br />
<input name="moname" type="text" class="ed" id="brnu" />
<br>
Occupation <br />
<input name="moccu" type="text" class="ed" id="brnu" />
<br>
Father's Name <br />
<input name="faname" type="text" class="ed" id="brnu" />
<br>
Occupation <br />
<input name="foccu" type="text" class="ed" id="brnu" />
<br>
Religion <br />
<input name="religiom" type="text" class="ed" id="brnu" />
<br>
School Year<br>
<select name="syear" class="ed">
<option>School Year</option>
<?php
$N = 2020;
for($i=1991; $i < $N; $i++)
{
$p=$i+1;
echo '<option>'.$i.'-'.$p.'</option>';
}
?>
</select>
<br>
<input type="submit" name="Submit" value="save" id="button1" />
</form>
</div>
<div id="footer">
</div>
<div class="clearfix"></div>
</div>
</body>
</html>
this is my save_reg page
<?php
include('connect.php');
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str)
{
$str = #trim($str);
if(get_magic_quotes_gpc())
{
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$mname = clean($_POST['mname']);
$email = clean($_POST['email']);
$gender = clean($_POST['gender']);
$status = 'pending';
$bday = clean($_POST['month']).'/'.clean($_POST['day']).'/'.clean($_POST['year']);
$level = clean($_POST['level']);
$paymode = clean($_POST['paymode']);
$fincome = clean($_POST['fincome']);
$moname = clean($_POST['moname']);
$moccu = clean($_POST['moccu']);
$faname = clean($_POST['faname']);
$foccu = clean($_POST['foccu']);
$sec='none';
$ppic='images/profic.png';
$add = clean($_POST['address']);
$age = clean($_POST['age']);
$religiom = clean($_POST['religiom']);
$syear = clean($_POST['syear']);
$result = mysql_query("SELECT * FROM tuetion WHERE level='$level'");
while($row = mysql_fetch_array($result))
{
if ($paymode=='Full Payment'){
$payables=($row['payable']-($row['payable']*.05));
}
else if ($paymode=='Installment'){
$payables=$row['payable'];
}
}
mysql_query("INSERT INTO prereg (fname, lname, mname, level, gender, paymentmode, payable, mother, m_occupation, father, f_occopation, family_income, section, status, email, bday, address, age, religion, ppic, schoolyear)
VALUES ('$fname','$lname','$mname','$level','$gender','$paymode','$payables','$moname','$moccu','$faname','$foccu','$fincome','$sec','$status','$email','$bday','$add','$age','$religiom','$ppic','$syear')");
header("location: pre_reg.php");
?>
it saves data but saves even blanks I want it to validate, before inserting
Put a name on your form:
<form action="save_pre_reg.php" method="post" onsubmit="return validateForm()" name="registrationform">
Validate the form before sending:
function validateForm()
{
if (document.registrationform.fname == '') {
alert('Please type your first name');
return false;
}
else if (document.registrationform.lname == '') {
alert('Please type your first name');
return false;
}
// more conditions here
else {
alert("Your ID number and password will send to your email address when the casher confirm your enrollment");
return true;
}
}
I recommend a simple (e.g. jQuery) solution, if you don't want to use a plugin:
$(document).on("submit", "form", function() {
var form = $(this);
if (form.find("input[name=fname]").val() == '') {
alert('Error: FNAME is empty!');
return false; // important
}
// more validation
});
By the way:
Don't use the id-attribute several times with the same value. It has to be unique. Otherwise use the class-attribute.
Only post relevant code (form + js) next time
Validate your HTML.
Good luck!

Integrate jquery ajax form and json

I'm beginer to web developing so maybe this is a so easy and stupid question from my side but I tring to find a way to solve this all day and please HELP.
I have a form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
font-family: Calibri;
}
.style1 {font-size: 24px};
-->
#wrapper {
margin: 0 auto;
width:600px;
height:1000px;
margin-top:50px;
background:url(bgMob.jpg);
border:solid;
border-color:#CCCCCC;
}
#inside {
width:920px;
height:1500px;
margin-left:30px;
margin-right:30px;
}
.style2 {color: #CC0000}
.style4 {color: #FF6600}
.style5 {color: #FF0000}
.styleField {
border-radius:5px;
border-width:thin;
border-color:#CCCCCC;
height:30px;
font-family:Calibri;
font-size:16px;
}
.styleField:hover {
background:#CC0000;
font-style:normal;
color:#FFFFFF;
}
.styleButton {
background:#EEEEEE;
border-radius:5px;
border-width:thin;
border-color:#CCCCCC;
height:30px;
}
.styleButton:hover {
background:#CCCCCC;
}
</style></head>
<body>
<div align="center" class="style1 style2"><span class="style5">creator</span><span class="style4"></span></div>
<div id="wrapper"><div id="inside">
<form id="city" name="city" action="<?php echo $_POST["city"]; ?>" method="post">
<label>CITY name :<br />
<input name="city" type="text" class="styleField" size="50" />
</label>
<label>
<input name="city" type="submit" class="styleButton" value="Check!" />
</label>
</form>
<form id="form1" name="form1" method="post" action="">
<label>Enter the site URL:<br />
<input name="textfield" type="text" class="styleField" size="50" />
</label>
<label>
<input name="Submit" type="submit" class="styleButton" value="Check!" />
</label>
</form>
<hr />
<form id="form0" name="form0" method="post" action="">
<label>Enter the URL where is all deals:<br />
<input name="textfield0" type="text" class="styleField" size="50" />
</label>
<label>
<input name="Submit0" type="submit" class="styleButton" value="Check!" />
</label>
</form>
<hr />
<form id="form2" name="form2" method="post" action="">
<label>
Xpath location of links where is ALL deals<br />
<input name="textfield2" type="text" class="styleField" size="50" />
</label>
<input name="Submit2" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form3" name="form3" method="post" action="">
<label>
Xpath location of TITLE <br />
<input name="textfield3" type="text" class="styleField" size="50" />
</label>
<input name="Submit3" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form33" name="form33" method="post" action="">
<label>
Xpath location of SUB-TITLE <br />
<input name="textfield33" type="text" class="styleField" size="50" />
</label>
<input name="Submit33" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form4" name="form4" method="post" action="">
<label>
Xpath location of PRICE <br />
<input name="textfield4" type="text" class="styleField" size="50" />
</label>
<input name="Submit4" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form5" name="form5" method="post" action="">
<label>
Xpath location of Original PRICE <br />
<input name="textfield5" type="text" class="styleField" size="50" />
</label>
<input name="Submit5" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form6" name="form6" method="post" action="">
<label>
Xpath location of DISSCOUNT <br />
<input name="textfield6" type="text" class="styleField" size="50" />
</label>
<input name="Submit6" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form7" name="form7" method="post" action="">
<label>
Xpath location of MAP - latitude, longitude <br />
<input name="textfield7" type="text" class="styleField" size="50" />
</label>
<input name="Submit7" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form8" name="form8" method="post" action="">
<label>
Xpath location offer TIME <br />
<input name="textfield8" type="text" class="styleField" size="50" />
</label>
<input name="Submit8" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form9" name="form9" method="post" action="">
<label>
Xpath location of BUSINESS name <br />
<input name="textfield9" type="text" class="styleField" size="50" />
</label>
<input name="Submit9" type="submit" class="styleButton" value="Check!" />
</form>
<hr />
<form id="form10" name="form10" method="post" action="">
<label>
<div align="center">
<input name="Submit10" type="submit" class="styleButton" value="Create a scraping file!" />
</div>
</label>
</form>
<hr />
</div>
</div>
</body>
</html>
and I have a second php file who i want to execute with jquery and based on entered xpath in form...
here is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Scraping - eKupon.ba</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
require_once('../includes/php.scarping.php');
//Adresa sajta
$scrapeConf['homePage'] = 'http://www.ekupon.ba';
//Adresa gdje se nalaze sve ponude
$scrapeConf['mainURL'] = 'http://www.ekupon.ba/aktivne-ponude/';
//XPATH putanja do linkova od svake ponude ponaosob
$scrapeConf['PonudaLinkovi'] = "//div[#class='pdodatna-naslovbox']/a/#href";
//XPath Putanja do naslova ponude
$scrapeConf['PonudaNaslov'] = "//div[#id='naslovbox']/span[#class='naslov']";
//XPath Putanja do podnaslova ponude
$scrapeConf['PonudaPodnaslov'] = "//div[#id='naslovbox']/span[#class='podnaslov']";
//XPath Putanja do prve slike koju zelimo
$scrapeConf['PonudaSlika'] = "//*[#id='ponuda']//a/img/#src";
//XPath putanja do polja sa cijenom
$scrapeConf['PonudaCijena'] = "//*[#id='ponuda']//div[#class='cijena']";
//XPath putanja do polja sa orginalnom cijenom
$scrapeConf['PonudaOrgCijena'] = "//*[#id='podaci']//span";
//XPath putanja do polja sa orginalnom cijenom
$scrapeConf['PonudaPopust'] = "//table[#id='podaci']//tr[2]/td[2]/span";
//Xpath do dijela koji sadrzi ili <script> sa lat lng ili url sa lat|lng
$scrapeConf['PonudaLatLng'] = "//table[#id='lokacija']//td/script[2]";
//Xpath putanja do elementa koji sadrzi vrijeme do istka ponude
$scrapeConf['PonudaVaziDo'] = "//table[#class='sjena']//span[#class='sat']";
//Xpath putanja do dijela koji sadrzi ime firme (nije obavezno)
$scrapeConf['PonudaFirmaIme'] = "//table[#id='lokacija']//p/strong";
//Xpath putanja do dijela koji sadrzi adresu firme (nije obavezno)
$scrapeConf['PonudaFirmaAdresa'] = "";
//Xpath putanja do dijela koji sadrzi telefon (nije obavezno)
$scrapeConf['PonudaFirmaTel'] = "";
//Xpath putanja do dijela koji sadrzi email (nije obavezno)
$scrapeConf['PonudaFirmaMail'] = "";
//Uzmi sve ponude
$ponudeList = xpathPonude($scrapeConf);
//DODAVANJE PONUDA
foreach ($ponudeList as $key => $ponuda) {
//OVDE obraditi varijable iz ponuda ako ima potrebe...
$ponuda['naslov'] = str_replace(' -', '', $ponuda['naslov']);
//sacuvaj sliku
$imeSlike = date('YMDHM-') . $ponuda['lat'] .'.jpg';
save_image($ponuda['slika'], '/home/pluspon/public_html/slike/'. $imeSlike);
$ponuda['slika'] = $imeSlike;
// DO OVE LINIJE SVE PONUDE I VARIJABLE MORAJU BITI
// SREDJENE I SPREMNE ZA UPIS U BAZU
print insertNew($ponuda['naslov'], $ponuda['podnaslov'], $ponuda['lat'], $ponuda['lng'], $ponuda['slika'], $ponuda['url'], $ponuda['popust'], $ponuda['vaziDo'], $ponuda['firma'], $ponuda['firmaadresa'], $ponuda['firmaemail'], $ponuda['firmatelefon'], $ponuda['firmaURL']);
}
//Debug vreme
print 'Vreme proteklo '. sk_genTime() .' sekundi';
?>
</body>
</html>
also what I want is when user type xpath in form and click on check then get a $result from php file and know is the xpath right or not. Offcource the result print right on the button. I know that can do it with ajax and jquery but I'm not very well in this so I also want to pay to someone who want to help me. And really sorry for my english but I'm working now on my engslih hard.
also I need to replace a text in apostrof (') with value from form...
Add an onSubmit parameter to the form tag, which calls a JavaScript function. That JavaScript function should call the PHP script by using the JQuery post function, and passing it the form data. The success callback of the post function will receive the data returned by the PHP script, and you can put that anywhere on your calling page via the DOM.

Categories