ctimings.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="cstyle.css">
// <script type="text/javascript">
// function validate(form)
// {
// if(document.myForm.sno.value == "")
// {
// alert("Please Complete Serial NUmber");
// return false;
// }
// if(document.myForm.ssign.value == "")
// {
// alert("Please Complete Signature");
// return false;
// }
// if(document.myForm.empName.value == "")
// {
// alert("Please Complete Employee Name");
// return false;
// }
// if(document.myForm.empSign.value == "")
// {
// alert("Please Complete Employee Signature");
// return false;
// }
// if(document.myForm.empCode.value == "")
// {
// alert("Please Complete Employee Code");
// return false;
// }
// }
// </script>
<title>Conference Room Timings</title>
</head>
<body>
<form action = "ctimings.php" method = "post" name = "myForm" onsubmit="return validate(this);">
<legend>ADD DETAILS</legend>
<fieldset>
<label>Serial Number <input type = "int" name = "sno" size="20" class = "required" /></label><br/>
<label>Security Signature <input type = "text" name = "ssign" size="20" class = "required" /></label><br/>
<legend>Employee Details</legend>
<fieldset>
<label>Employee Name <input type = "text" name = "empName" size="20" class = "required" /></label><br/>
<label>Employee Signature <input type = "text" name = "empSign" size="20" class = "required"/></label><br/>
<label>Employee Code <input type = "int" name = "empCode" size="20" class = "required" /></label><br/>
</fieldset>
<legend>Date & Time</legend>
<fieldset>
<label>Date <input type = "int" name = "date" size="20" class = "required"/></label><br/>
<label>In - Time <input type = "int" name = "inTime" size="20" class = "required"/></label><br/>
<label>In - Time <input type = "int" name = "outTime" size="20" class = "required"/></label><br/>
</fieldset>
<legend>Usage</legend>
<fieldset>
<label>
Phone Set Usage
<input type = "radio" name = "radio1" value = "YES" />
<input type = "radio" name = "radio1" value = "NO" /><br/>
</label>
<label>
Projector Usage
<input type = "radio" name = "radio2" value = "YES" />
<input type = "radio" name = "radio2" value = "NO" /><br/>
</label>
</fieldset>
<input type = "submit" value = "SUBMIT" class = "" name = "SUBMIT"/>
</fieldset>
</form>
</body>
</html>
ctimings.php
<html>
<body>
<?php
if(isset($_POST['SUBMIT']))
{
echo "pressed";
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "ibm";
$con=mysql_connect("localhost","root","","ibm");
// Check connection
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
else
{
echo 'Connection was OK';
}
$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) {
die ('Can\'t use ibm : ' . mysql_error());
}
else
{
echo "Database was OK!\n";
}
$sql = "INSERT INTO ctimings(sno,ssign,empName,empSign,date,intime,outtime,empCode,Proj.Use,Ph.Use) VALUES('$_POST[sno]','$_POST[ssign]','$_POST[empName]','$_POST[empSign]','$_POST[date]','$_POST[inTime]','$_POST[outTime]','$_POST[empCode]','$_POST[radio1]','$_POST[radio2]')";
mysql_query($sql,$link);
mysql_close($link);
}
else
echo 'error';
?>
</body>
</html>
It is a simple proceedure but i am not able to narrow down as to why is not the data being sent to the database as the phpmyadmin table "ctimings" shows that it has no record.After the submit button has been pressed it should post the data to the database but in vain.The database name and table names in the database are correct.
Your sql string is incorrect, it needs to be;
$sql = "INSERT INTO ctimings(sno,ssign,empName,empSign,date,intime,outtime,empCode,Proj.Use,Ph.Use) VALUES('".$_POST[sno]."','".$_POST[ssign]."','".$_POST[empName]."','".$_POST[empSign]."','".$_POST[date]."','".$_POST[inTime]."','".$_POST[outTime]."','".$_POST[empCode]."','".$_POST[radio1]."','".$_POST[radio2]."')";
To add onto the answers above, it might be an idea to sanitise your data.
$_POST = array_map('mysql_real_escape_string', $_POST);
your query would be like
$sql = "INSERT INTO ctimings(sno,ssign,empName,empSign,date,intime,outtime,empCode,Proj.Use,Ph.Use) VALUES
('".$_POST[sno]."','".$_POST[ssign]."','".$_POST[empName]."','".$_POST[empSign]."','".$_POST[date]."',
'".$_POST[inTime]."','".$_POST[outTime]."','".$_POST[empCode]."','".$_POST[radio1]."'
,'".$_POST[radio2]."')";
And you need to escape the post variables using mysql_real_escape_string
And main reason is due to Proj.Use,Ph.Use you are using insert into two tables at a time I think,
$_POST is a Global Array you have to use it like this $_POST["field_name"];
Related
So basically I am trying to upload a Model and Serial number into a database only on POST but SOMETIMES, not all the time, it will duplicate the entry in the database even though I only hit submit once, once submitted, the text field goes blank as it should so how is it adding it more than once?
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
if ($_POST['submit'])
{
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$mod = mysqli_real_escape_string($con,$_POST['model']);
$sn_num = mysqli_real_escape_string($con,$_POST['sn']);
$sql = "INSERT INTO rma_product (m_type,pro_sn) VALUES ('$mod','$sn_num')";
if ($con->query($sql) === TRUE) {
$success = "New record created successfully";
} else {
$success = "Failed!";
}
$con->close();
}
?>
<form method="post" name="sn_upload" id="sn_upload">
<div>
<label for="model">Model: </label><select id="model" name="model" title="Model" />
<option value="ModelA">ModelA</option>
</select><br><br>
<label for="sn">Serial Number: </label><input type="text" id="sn" name="sn" placeholder="Serial Number" pattern="[a-zA-Z0-9]{11,13}"/><br><br>
<input name="submit" type="submit" value="Submit" />
<?php if( $success) echo "<div>".$success."</div>";?>
</div>
</form>
<script>
(function() {
var input = document.getElementById('sn');
var form = document.getElementById('sn_upload');
var elem = document.createElement('div');
elem.id = 'notify';
elem.style.display = 'none';
form.appendChild(elem);
input.addEventListener('invalid', function(event){
event.preventDefault();
if ( ! event.target.validity.valid ) {
input.className = 'invalid animated shake';
elem.textContent = 'Serial Number must be between 11 and 13 characters and DO NOT include spaces.';
elem.className = 'error';
elem.style.display = 'block';
}
});
input.addEventListener('input', function(event){
if ( 'block' === elem.style.display ) {
input.className = '';
elem.style.display = 'none';
}
});
})();
</script>
I have a form like this: The file name is "main.php"
<html>
<form action = "options.php" method = "POST" />
<p> <h3> Enter Phone Number: </h3> <input type = "text" name =
"cust_phone" />
<p> <input type = "submit" value = "Submit" />
</form>
</html>
The "options.php" looks like this:
<html>
<body> Details of: <?php session_start();
echo htmlentities($_POST["cust_phone"]) . "<br>";
$link = oci_connect('hd','hd', 'localhost/mydb');
if(!$link) {
$e = oci_error();
exit('Connection error ' . $e['message']);
}
$_SESSION['var'] = $_POST["cust_phone"];
echo $_SESSION['var'];
$ph = htmlentities($_POST["cust_phone"]);
$q1 = "select CUST_ID from customer where CUST_PHONE = :bv_ph";
$q1parse = oci_parse($link, $q1);
oci_bind_by_name($q1parse, ':bv_ph', $ph);
oci_execute($q1parse);
oci_fetch($q1parse);
$res = oci_result($q1parse, 'CUST_ID');
if(!$res) {
echo "No Order found. New Order?";
}
?>
<form action = "" method = "POST" >
<input type = "radio" name = "option" value = "Yes" checked> Yes
<br>
<input type = "radio" name = "option" value ="No"> No <br>
<input type = "submit" value = "submit">
</form>
<?php
if(isset($_POST['option']) && ($_POST['option']) == "Yes") {
header("Location: newcustomer.php");
}
elseif(isset($_POST['option']) && ($_POST['option']) == "No") {
header("location: main.php");
}
?>
</body>
The "newcustomer.php" looks like this:
<!DOCTYPE HTML>
<html>
<body>
<form action = "order.php" method = "POST" >
<p> Phone Number: </p>
<p> Enter Address: <input type = "text" name = "address" /></p>
<p> Enter Area: <input type = "text" name = "area" /></p>
</form>
<?php
session_start();
echo $_SESSION ['var'];
?>
</body>
</html>
I want the value of the number entered by the user in "main.php" to be used in "newcustomer.php" which I am not able to achieve. Am i using the session variable correctly? The result when I run "newcustomer.php" does not show any error but does not echo the "$_SESSION['var']".
You should write session_start() every page topper ...
<?php
session_start();
?>
<html>
.....
......
login.php
<?php
//get post data
$user = trim($_POST['user']);
$pass = trim($_POST['pass']);
$nume = $_POST['nume'];
$class = $_POST['class'];
// Here I connect to the database and search for $user
// if $user is found, i want to:
alert("succes"); // optional, just a reasurance
$name =; // the name of the $user in database
$class =; // the class of the $user in database
// here i want to return the information that i need for main page
echo $name;
echo $class;
?>
a.html
<html>
<head>
<script type="text/javascript">
function checkForm(){
var name = document.forms[0].user.value;
if (name.length < 3) {
alert("Username is too short");
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form action="login.php" method="POST" onsubmit="return checkForm()">
User: <input type="text" name="user"/>
Password: <input type="password" name="pass"/>
<input type="hidden" name="nume"/>
<input type="hidden" name="class"/>
<input type="submit"/>
</form>
</body>
</html>
How do I store the return values from login.php in two variables that i need to further change my main page?
For example how can I have in the main page X = $nume and y = $class with values from login.php?
I had previously mentioned about my registration code. I now want to save the image name into the database.The image is to be uploaded using file upload. The validation is done using jquery and the form has to be submitted using ajax.Now i had read that file uploads cannot be performed using ajax.And that is the problem with my code.
Following is my code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
function ValidateEmail(email) {
var expr = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)| (([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
return expr.test(email);
};
$(document).ready(function(){
$('#submit').on('click', function() {
//e.preventDefault();
valid = true;
if ($('#name').val() == '') {
alert ("please enter your name");
//$('#errorMsg1').css('color','red');
//$('#errorMsg1').html('Please enter your name');
//document.getElementById("errorMsg1").innerHTML = "You must enter a name";
valid = false;
}
if ($('#email').val() == '') {
alert ("please enter your email");
//$('#errorMsg2').css('color','red');
//$('#errorMsg2').html('Please enter your emailid');
//document.getElementById("errorMsg2").innerHTML = "You must enter a email";
valid = false;
}
if (!ValidateEmail($("#email").val())) {
alert("Invalid email address.");
//document.getElementById("errorMsg2").innerHTML = "Invalid email address.";
}
if ($('#bday').val() == '') {
alert ("please enter your birth date");
//$('#errorMsg3').css('color','red');
//$('#errorMsg3').html('Please enter your birth date');
//document.getElementById("errorMsg3").innerHTML = "You must enter your birth-date";
valid = false;
}
if ($('#gender').val() == '') {
alert ("please select your gender");
//$('#errorMsg4').css('color','red');
//$('#errorMsg4').html('Please select your gender');
//document.getElementById("errorMsg4").innerHTML = "You must select your gender";
valid = false;
}
if ($('#image').val() == '') {
alert ("please select an image");
//$('#errorMsg5').css('color','red');
//$('#errorMsg5').html('Please select an image');
//document.getElementById("errorMsg5").innerHTML = "You must select an image";
valid = false;
}
});
return false;
$("#multiform").submit(function (e)
{
var formObj=$(this);
var formURL=formObj.attr("action");
var formData=new formData(this);
$ajax({
URL:formURL,
type:'POST',
data:formData,
mimeType:"multipart/form-data",
contentType:false,
cache:false,
processData:false,
success:function(data,textStatus,jqXHR){
},
error:function(jqXHR,textStatus,errorThrown){
}
});
e.preventDefault();
e.unbind();
}
);
return true;
$("#multiform").submit();
});
</script>
</head>
<body>
<form name="multiform" id="multiform" action="save_data.php" method="POST" enctype="multipart/form-data">
<h4>Name:</h4> <input type="text" name="name" id="name" autofocus>
<br><br>
<h4>E-mail:</h4> <input type="text" name="email" id="email" autofocus>
<br><br>
<h4>Birth-date:</h4> <input type="text" name="bday" id="bday" autofocus>
<br><br>
<h4>Gender:</h4>
<input type="radio" name="gender" id="gender" value="female" autofocus>Female
<input type="radio" name="gender" id="gender" value="male" autofocus>Male
<br><br>
Select Image:<input type="file" name="image" id="image"><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
save_data.php:
<!DOCTYPE html>
<html>
<body>
<?php
error_reporting( E_ALL & ~E_NOTICE );
// define variables and set to empty values
$name = $email = $bday = $gender = $image="";
//$image=$_FILES["image"]["name"];
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$bday=$_POST["bday"];
$gender = $_POST["gender"];
$image =$_POST["image"];
$servername = "localhost";
$username = "mvs1";
$password = "";
$dbname="scootsy_categories";
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$sql = "INSERT INTO user_details(name,email,bday,gender,image) VALUES ('$name','$email','$bday','$gender','$image')";
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//if($name !="" || $email !="" || $bday !="" || $gender !="" || $image !="" )
//{
// $sql="DELETE FROM user_details where name='$name' || email='$email' || bday='$bday' || gender='$gender' || image='$image'";
//}
$conn->close();
}
echo "Successful";
?>
</body>
</html>
You can't save images into a database, but you can save the path url and before you save the image into a folder with php. For do this, you have to modify your ajax code and your php file because for do all this, we have to use $_FILES for extract the name, type, size and temp_folder for move it into a filder.
Rewrite your ajax code.
Get with $_FILES the properties of the file
<script type="text/javascript">
$('body').on('click','#submit', function() {
$.ajax({
type:'POST',
url:'save_data.php',
data: new FormData($('#your_id_form')[0]),
contentType:false,
cache:false,
processData:false,
success:function(upload) {
$('#multiform').html(upload):
}
});
});
</script>
<form id="your_id_form" method="POST" action="save_data.php">
<input type="name" placeholder="Your name">
<input type="file" name="file">
<input type="button" id="submit">
</form>
<div id="multiform"></div>
//save_data.php
$name = $_POST['name'];
$file_name = $_FILES['file'];
$file_size = $file_name['size'];
$file_type = $file_name['type'];
$file_tmp = $file_name['tmp_name'];
list($name,$type) = explode('/',$file_type);
$save_temp = $file_tmp;
//create a new folder named images into your root page
$new_path = 'images/'.$name.$type;
//we move the file from the temporal folder to a new folder
move_uploaded_file($save_temp, $new_path);
$file_url = 'images/'.$file_type;
$sql = "INSERT INTO database (name,file_url) VALUES ('$name', '$new_path')";
//do stuffs for connect your query to your database
echo 'Uploaded!';
?>
I am currently using this php form to submit into our mySQL database with a "chip_number" and "order_number" also with a date and time stamp. We want to use this with no keyboard or mouse, just a scanner. Currently it tabs the first field and when the second field is scanned the form is submitted, which is working as intended but it completely starts the form over, i would like it to keep the first field (order_number) after submitting so we can scan multiple "chip_numbers" on the same "order_number" then have a Master submit button if you will to send it all through when the employee is done with that order number and start with a blank form. This is the script i am using. thanks to all in advance!
<!-- Insert -->
<?php
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MICROCHIP_TBL (chip_number,order_number)
VALUES
('$_POST[chip_number]','$_POST[order_number]')";
IF (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
mysqli_close($conn);
?>
<html>
<head>
<!-- Validate form function -->
<!--<script type="text/javascript">
// function validateForm()
// {
// var x=document.forms["chip_insert"]["order_number"].value;
// var y=document.forms["chip_insert"]["chip_number"].value;
// if (x==null || x=="")
// {
// alert("Please enter an Order Number.");
// document.forms["chip_insert"]["order_number"].focus();
// return false;
// }
// if (y==null || y=="")
// {
// alert("Please enter a Microchip Number.");
// document.forms["chip_insert"]["chip_number"].focus();
// return false;
// }
// }
</script>
-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
function getNextElement(field) {
var form = field.form;
for ( var e = 0; e < form.elements.length; e++) {
if (field == form.elements[e]) {
break;
}
}
return form.elements[++e % form.elements.length];
}
function tabOnEnter(field, evt) {
if (evt.keyCode === 13) {
if (evt.preventDefault) {
evt.preventDefault();
} else if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.returnValue = false;
}
getNextElement(field).focus();
return false;
} else {
return true;
}
}
</script>
</head>
<body onLoad="document.chip_insert.order_number.focus();">
<center>
<h1>Jeffers HomeAgain Microchip Entry</h1>
<form name="chip_insert" id="chip_insert" action="<?php echo $PHP_SELF;?>" onsubmit="return validateForm()" method="post">
Order Number: <input tabindex="1" maxlength="11" type="text" name="order_number" id="order_number" required="required"onkeydown="return tabOnEnter(this,event)" /><br /><br />
Tag Number: <input tabindex="2" maxlength="15" type="text" name="chip_number" id="chip_number" required="required" /><br /><br />
<input tabindex="7" type="submit" />
</center>
</form>
header('Location: http://JVSIntranet/microchip/homeagain.php');
This code redirects back to the form, I guess. You should add the ordernumber so it can be picked up by the form.
$ordernr = $_POST['order_number'];
header("Location: http://JVSIntranet/microchip/homeagain.php?order_number=$ordernr"); //mark the double quotes
in your form code you will have to use something like
<?php $value = (isset($_GET['order_number'])) ? " value=$_GET['order_number'] " : ""; ?>
Order Number: <input tabindex="1" maxlength="11" type="text" name="order_number" id="order_number" <?php echo $value; ?> required="required"onkeydown="return tabOnEnter(this,event)" /><br /><br />
I finally got it. i had to take out the Return function from my form and i added this to my script:
$value = "";
if( isset( $_POST ["order_number"] )) $value = $_POST ["order_number"];
then i put this in my input line and it works fine:
value="<?php echo $value; ?>"