php script for html form is not working - php

hi people i have these two scripts for fill a database table on my PC , with an HTML form . the HTML code works perfect but the php it just does not work , i have specified everything ,but it just doesn't work , when i give click on submit the browser just pop ups a download option but nothing else...
<html>
<head>
<tittle>
<h1 > <b> FORMAT FOR TECHNICAL ISSUES
<link rel="stylesheet" href="/var/www/html/proyecto/css/styles.css">
</b> </h1>
</tittle>
<hr> </hr>
</head>
<body>
<div>
<img src="logo.png" width=100% /img>
<hr> </hr>
<form method="POST" action="">
<input type="text" placeholder="NOMBRE" name="Nombre" maxlenght="30" size="20px" id="nombre" >
<input type="text" name="Extension" placeholder=EXTENSION maxlength="5" id="extension">
<br>
<hr> </hr>
<br>
<br>
<h2> CAMPAIGN /CAMPAÑA </h2>
<br>
<label for="Campaign"> Select your campaign:</label>
<select name="Campaign" id="campaign" >Campaign
<option value="Procall">Procall</option>
<option value="Tigo">Tigo</option>
<option value="Spanish">Spanish</option>
</select>
<hr> </hr>
<h2> KIND OF FAILURE </h2>
<label for="Tipo de incidente">Select the type of incident:</label>
<select id="incident" name="incidente" placeholder="Tipo de incidente">incident
<option value="Slow Computer">Slow Computer</option>
<option value="Headset fail"> Headset Fail </option>
<option value="Zoiper Issue">Zoiper Issue </option>
<option value="Crm Issue">Crm Issue </option>
<option value="Spark Issue">Spark Issue </option>
<option value="Vc Dialer Issue">VC Dialer Issue </option>
<option value="Network Issue">Network Issue </option>
</select>
<input type"text" id="description" name="Description" placeholder=DESCRIPTION style="width:300px;height:100px">
<br></br>
<h2>FAILURE DATE </h2>
<label for="failure date"> Enter the day and hour when failure occurred :</label>
<input name="failure_date" type="date-time-local" placeholder="yyyy/mm/dd/hh/mm" id="failure date" min="2018-05-01T08:30" max="2018-12-30T22:30" >
<br>
<hr></hr>
<label for="did you lose a call?" id="lost_call" >Did you lose any call?:</label>
<br>
<input type="radio" name="lost call" value="yes " id="lost_call" > Yes , i lost a call.
<br>
<input type="radio" name="lost_call" value="no" id="lost_call" >No i have not lost any call
<br
<br>
<label id="lost docmuent" for="did you lose any document?"> Did you lose any document or information?:</label>
<br>
<input type="radio" name="document" value="yes"> Yes i lost one or more documents
<br>
<input type="radio" name="document" value="no"> No have not lost any document.
<br>
<br>
<label for="did you have to log out?" id="logout" > Did you have to logout?:</label>
<br>
<input type="radio" name="logout" value="yes">Yes i had to logout
<br>
<input type="radio" name="logout" value="no">no i didn't have to logout
<br>
<br>
<br>
<br>
<input type="submit">
</form>
</div>
</body>
</html>
and here is the php script for the action and connect to the sql database
<?php
$con = mysql_connect("localhost","root","m0l0t0v" );
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("it", $con);
$sql="INSERT INTO tec-issues (Nombre,Extension ,Campaign ,incidente,Description,failure_date,lost_call,document,logout )
VALUES
('$_POST[Nombre]', '$_POST[Extension]', '$_POST[Campaign]', '$_POST[incidente]', '$_POST[Description]', '$_POST[failure_date]', '$_POST[lost_call]', '$_POST[document]', '$_POST[logout]' )";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
PLEASE HELP ME EVERYTHING IS CORRECT , BUT IT JUST DONT WORK

Check syntax for mysql_select_db it should be mysql_select_db($con,"dbname") and please use mysqli prefix instead of mysql because it is safer.
I recommend using msqli_escape_string for every data you get from POST or GET to protect from sql injection

Related

Fill HTML5 input field with current date + 14 days

The user is making a coin submission and when they go to do that I want the date to load into a input field that I'm using to track the time the submission was made. That date will be recorded in a mySQL database. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. I included the entire form so you could get a bigger picture. Feel free to make adjustments. Thanks any help is greatly appreciated.
I'm able to call a javascript function and have the user select the date from a calendar, but that's not what I need. Javascript - get a date from an HTML input but I'm not sure how to concatenate the +14 days for the next cut off date.
CoinSubmission.html
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
AdminCoinSub_Code.php
<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO admincoinsubmission (Store, Position, Nickname, Contact, MachineCount, CutOffDate, Coins, location)
VALUES ('$_POST[Store]','$_POST[Position]','$_POST[Nickame]','$_POST[Contact]','$_POST[MachineCount]','$_POST[CutOffDate]','$_POST[Coins]','$_POST[location]')");
$stmt->bindParam(':Store', $Store);
$stmt->bindParam(':Position', $Position);
$stmt->bindParam(':Nickname', $Nickname);
$stmt->bindParam(':Contact', $Contact);
$stmt->bindParam(':MachineCount', $MachineCount);
$stmt->bindParam(':CutOffDate', $CutOffDate);
$stmt->bindParam(':Coins', $Coins);
$stmt->bindParam(':location', $location);
$stmt->execute();
echo "Success";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
When the page loads the date row in the coin submission form should display the cutoffdate.
date = (current date + cutoffdate)
You can achieve the following task using similar code -
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Date +14 days</title>
</head>
<body>
<input type="date" name="date1" id="date1" value="" />
<input type="text" name="date2" id="date2" value="" />
<script type="text/javascript">
var date1 = document.getElementById("date1");
date1.addEventListener('change', function(){
tempDate = new Date(date1.value);
finalDate = tempDate.setDate(tempDate.getDate() + 14);
console.log(new Date(finalDate));
});
</script>
</body>
</html>
To achieve expected result, use below option of add time with getTime() method and add 14 days
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
Working code sample for reference
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
codepen - https://codepen.io/nagasai/pen/VJWmNE?editors=1010

Says Updated Successfully But When I look At The Database It's Not

Can you guys double check my codes. When I clicked the Update button it told me that the update was successful but when I look at my database nothing changed. I know I'm doing something wrong. Hope you guys help me to figure it out. Thanks.
I am still learning and it's an honor to learn from you guys. Please be nice and use basic terms hehe
<html>
<body>
<div>
<form action="edit.php?id=<?php echo $id;?>" method="post">
<div>
<label for="prov">Provinces</label><br>
<select id="prov"type="text" name="provi1">
<option><?php echo $provi; ?></option>
<option value="Isabela">Isabela</option>
<option value="La Union">La Union</option>
<option value="Pangasinan">Pangasinan</option>
<option value="Ilocos Sur">Ilocos Sur</option>
<option value="Ilocos Norte">Ilocos Norte</option>
</select>
</div>
<div>
<label>Date</label><br>
<input type="date" name="date" value="">
</div>
<div>
<label>Typhoon Name</label><br>
<input type="text" name="typhoon" value="<?php echo $typhoon; ?>">
</div>
<div>
<label>Warning #</label><br>
<input type="text" name="warning" value="<?php echo $warning; ?>">
</div>
<div>
<label for="prov">SS Height</label><br>
<label>2-3 meters
<input type="radio" name="meter" value="2-3 Meters"
<?php if ($ssh == '2-3 Meters') echo 'checked="checked"'; ?>>
</label>
<label>1-2 meters
<input type="radio" name="meter" value="1-2 Meters"
<?php if ($ssh=='1-2 Meters') echo 'checked="checked"'; ?>>
</label>
<label>< 1 meter
<input type="radio" name="meter" value="< 1 Meter"
<?php if ($ssh=='< 1 Meter') echo 'checked="checked"'; ?>>
</label><br>
<label for="imp">Impacts</label><br>
<textarea id="imp" type="text" name="impact1"><?php echo $impact; ?></textarea>
<label for="adv" >Advice</label><br>
<textarea id="adv" type="text" name="advice1"><?php echo $advice; ?></textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" name="update">
<input type="reset" value="Cancel">
</div>
</form>
</div>
</body>
</html>
Fix the MySQL injection, or I WILL come and erase your database. (See comments under question).
Then (and only AFTER you fixed that):
You are using id from the $_GET, but for the update your ID comes from $_POST. However, you forgot to put that <input name='id' value='<?=$edit?>' type='hidden'> within your form. So there is no $_POST['id'], so it only updates all records with an empty id. And says: "Yay, I succesfully updated 0 records!". Hence no error: it was an overwhelming success. ;)
You seem to be confused about the id. There is no need for an $edit, an $id and an $u_id variable. It is all the same id. Your<FORM ACTION=... actually puts ?id= (empty string) behind the URL, since you emptied $id.

select tag name not working while using it in php

I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.

Same page form submit very slow to display data

I have a Joomla site that I am building a form for. The form is working fine in a localhost test environment but when I put it on my live server, the form would not work properly. I stripped out code until I basically just had the form and I still have issues, the form is very slow to submit and display the data, it could take minutes for the updated data to display and I have to refresh the page to get it. On another server the form works properly. Data is displayed as fast as the submit button is clicked.
I've been fighting this for a couple of days and have gotten nowhere.
Here is the form:
<form action="<?php $_SERVER['REQUEST_URI'] ?>" method="post">
<select name="vendor_num">
<option value="" selected="selected" > Select a Vendor </option>
<option value="000000" >All Vendors</option>
<option value="008064" >Great Glacier Salmon</option>
<option value="008173" >Seed 2 Sausage</option>
<option value="008219" >Back Forth Artisan Cheese</option>
<option value="008215" >True North Salmon</option>
</select>
<br />
<label for="prov_ont">
<input type="radio" id="prov_ont" name="prov" value="Ontario" > Ontario
</label>
<br />
<label for="country_canada">
<input type="radio" id="country_canada" name="country" value="Canada" > Canada
</label>
<br />
<label for="kosher">
<input type="radio" id="kosher_flag" name="kosher" value="Y" > Kosher
</label>
<br />
<label for="halal">
<input type="radio" id="halal_flag" name="halal" value="Y" > Halal
</label>
<br />
<label for="oceanwise">
<input type="radio" id="oceanwise_flag" name="oceanwise" value="Y" > Oceanwise
</label>
<br />
<input type="submit" name="submit" value="submit" style="padding:5px; margin-top:20px;">
<input type="reset" value="reset" name="reset" style="padding:5px; margin-top:20px;">
</form>
<pre>
<?php
var_dump($_POST);
echo "<br />";
?>
</pre>
the problem URL is: http://findlayfoods.com/search-test
Here is another host with the same page working:
http://glenntooth.com/index.php/form-test
I have also tried with action="", exactly the same thing.
I am admittedly a beginner but have been working on this for a while. Help appreciated.
G

I do not want to add new record while refreshing

I am junior programmer and stuck in making this simple form.
This is my script and every time I press f5, it will automatic add last same record as new record but I want to add only when submit button is pressed. Please help.
<?php
mysql_connect("localhost","root","") or die (mysql_error());
mysql_selectdb("test_database") or die (mysql_error());
if(isset($_POST['s1']))
{
$nm=$_POST["name"];
$gen=$_POST["gen"];
$add=$_POST["add"];
$tech=$_POST["tech"];
$iq=mysql_query("insert into formcheck (name,gender,address,tech) values ('{$nm}','{$gen}','{$add}','{$tech}' )") or die (mysql_error());
if($iq)
{
echo "<script> alert('Record inserted Successfully') </script>";
}
else
{
echo "Error occurred in inserting a record";
}
}
?>
<html>
<body>
<form method="post" name="f1">
<input type="text" placeholder="Enter your Name" name="name" required="">
<br>
<input type="radio" name="gen" required="">Male
<input type="radio" name="gen">Female
<br>
<textarea name="add" placeholder="Address" required="" rows="5" cols="20" draggable="true"></textarea>
<br>
<select name="tech" required="">
<option value=""> Select Technology </option>
<option value="p"> PHP </option>
<option value="i"> iPhone </option>
<option value="a"> ASP </option>
<option value="h"> HTML/CSS3 </option>
</select>
<br>
<input type="submit" value="Click here" name="s1">
</form>
</body>
</html>
Write all your php code in a separate file say- insert.php
and edit the <form> tag to-
<form method="post" action="insert.php" name="f1">
Just do a redirect after submitting data to the database. It's a common practise.
make a different file for insertion and then redirect back . this will solve the issue.
the $_post array need to be cleared un less duplicate enteries will be entered

Categories