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
Related
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
<body>
<span>
<div class="heading">
<h3><img src="http://zicom.com/img/ilayashop-1482233381.jpg" alt="Zicom Logo" ></h3>
<h1><i>Zicom </i> SMS Application</h1><br>
</span>
</div>
<br>
<div>
<form method='post' action=''>
<label for="fname">Contact Number</label>
<input type="text" id="fname" name="number" placeholder="Contact Number....">
<label for="sms" id="msg">Message</label>
<select id="country" name="message">
<option value="" name="message">Blank</option>
<option value="hello" name="message">Abandoned Call</option>
<option value="" name="message">Audit Call </option>
</select>
<input type="text"id="msg" name="message" placeholder="Type your Message...." >
<input type="submit" value="Send" name="send">
</form>
</div>
<?php
if(isset($_POST)) {
$n = $_POST['number'];
$m = $_POST['message'];
echo "$n $m";
}
?>
</body>
</html>
I have used the same method many times and i have succesfully sent the data to php file, but im unable to do the same. I try to print the content of form, but is not executing.
I have following form and I want to send the data to controller. The problem is that data from dynamically created inputs is not being passed to the controller. This is the output of dd($request->all());
`array:4 [▼
"_token" => "gzpNTHPfZl8GCtNbCPGJrc4S4zTUmhhMUve4gyet"
"course" => "ICTP"
"section" => "BB3"
"lecture" => "functions and pointers"
]
`
welcome.blade.php
`
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<META http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<div>
#if(count($errors)>0)
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<p>{{$error}}</p>
#endforeach
</div>
#endif
</div>
<div>
<form action="{{route('course.save')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<label>Select a category</label>
<select>
<option>Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<br><br>
<label>Select a subcategory</label>
<select>
<option>SubCategory 1</option>
<option>SubCategory 2</option>
<option>SubCategory 3</option>
<option>SubCategory 4</option>
</select>
<br><br>
<label for="course">Course Name:</label>
<input type="text" name="course" id="course"/>
<div id="content">
<div id="section-content-1">
<div id="secs">
<label for="section">Section 1:</label>
<input type="text" name="section" id="section"/>
</div>
<div id="lects">
<label for="lecture">Lecture 1:</label>
<input type="text" name="lecture" id="lecture"/>
</div>
<button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br>
</div>
</div>
<button type="button" id="add-sect" data-sect="1" >Add New Section</button>
<br><br><br><br>
<button class="btn-primary" type="submit">Save</button>
</form>
</div>
</body>
</html>
<script src="{{asset('/js/code.js')}}"></script>`
Here is the jquery file code.js
//$('#add-lect').click(function
$('#content').on('click', '#add-lect', function() {
var count = parseInt($(this).parent().children('#lects').children().length / 2) + 1;
lectures = '<label>Lecture ' + count + ':</label><input type="text" name="lecture" id="lecture"/>'
$(this).parent().find('#lects').append(lectures);
});
$('#add-sect').click(function(){
sect_id = parseInt($('#add-sect').attr('data-sect')) + 1;
$('#add-sect').attr('data-sect', sect_id)
var count = ($("#secs").children().length / 2) + 1;
//section = '<div id="section-content-'+sect_id+'"> <div id="secs"> <label for="section">Section'+sect_id+':</label><input type="text" name="section" id="section-"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button<br><br></div>'
section = '<div id="section-content-'+sect_id+'"><div id="secs"><label for="section">Section '+sect_id+':</label> <input type="text" name="section" id="section"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br></div>'
$('#content').append(section);
});
When you have a lot of input fields with same name, like
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
only the last input field will be passed to server.
To pass arrays of similar data there's special notation for name attribute:
<input type="text" name="section[]"/>
<input type="text" name="section[]"/>
<input type="text" name="section[]"/>
Using this, on server side you will have $_POST['section'] as array, which you can iterate and get values.
I also removed id="section" attribute as id attribute must be unique on a page.
So you need to change names of your inputs, both in blade template and in javascript code.
Update:
In case of one-to-many relations you can extend your naming to:
<input type="text" name="section[1]"/>
<input type="text" name="lecture[1][]"/>
<input type="text" name="lecture[1][]"/>
<input type="text" name="section[2]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="section[3]"/>
<input type="text" name="lecture[3][]"/>
<input type="text" name="lecture[3][]"/>
So, as you see - you can explicitly define index for names.
In above-mentioned case you can iterate over $_POST['section'] and get $_POST['lecture'] with same index as section.
Here's the code:
<html>
<head>
<title>emarket.com</title>
<link rel="stylesheet" type="text/css" href="./style.css"/>
</head>
<header>
<h1>emarket.com</h1>
<h1>Post Ad</h1>
</header>
<body>
<?php
if(!empty($_POST)){
$title=$_POST['title'];
$description=$_POST['description'];
$district=$_POST['district'];
$phone=$_POST['phone'];
$catagory=$_POST['catagory'];
$price=$_POST['price'];
mysql_connect("localhost","root","");
mysql_select_db("maindb");
mysql_query("INSERT into allads VALUES(NULL,'$title','$phone','$description', '$price','$district','$catagory')");
echo "<span style='background-color: deepskyblue;'>Your advert is now online!</span>";
}
?>
<form class="postad" method="post" action="">
<fieldset><caption><h2>Post New Advert</h2></caption></fieldset>
<fieldset>
<label>Title</label><br/>
<input type="text" name="title" class="form-text" required>
</fieldset>
<fieldset>
<label>Description</label><br/>
<input name="description" class="form-text" required>
</fieldset>
<fieldset>
<label>District</label><br/>
<select name="district"required>
<option selected>Colombo</option>
<option>Kandy</option>
<option>Matara</option>
<option>Galle</option>
</select>
</fieldset>
<fieldset>
<label>Category</label><br/>
<select name="catagory"required>
<option value="Electronics">Electronics</option>
<option value="Property">House and Property</option>
<option value="Auto">Automobiles</option>
<option value="Misc">Miscellaneous</option>
</select>
</fieldset>
<fieldset>
<label>Phone</label><br/>
<input type="tel" name="phone" maxlength="10" class="form-text" required>
</fieldset>
<fieldset>
<label>Price</label><br/>
Rs. <input type="number" name="price" class="form-text" required>
</fieldset>
<fieldset>
<input type="submit" name="submit" value="submit"/>
</fieldset>
</form>
</body>
</html>
This happened after I entered the PHP parts, it was visible in Firefox before that. Other files having PHP, like the home page, works fine. In chrome not only the website is visible, it works fine too. Stuff from the form gets inserted into the sql table without any problems.
Do you have the slightest idea of how vulnerable you are to mysql injection attacks? Please start using pdo. mysql_* is deprecated.
I've cleaned the function for you. The php part will only be executed once you click the submit button.
<?php
mysql_connect("localhost","root","");
mysql_select_db("maindb");
if(isset($_POST['submit']))
{
$title=isset($_POST['title']) ? $_POST['title'] : '';
$description=isset($_POST['description']) ? $_POST['description'] : '';
$district=isset($_POST['district']) ? $_POST['district'] : '';
$phone=isset($_POST['phone']) ? $_POST['phone'] : '';
$catagory=isset($_POST['catagory']) ? $_POST['catagory'] : '';
$price=isset($_POST['price']) ? $_POST['price'] : '';
if(!empty($title) && !empty($description) && !empty($district) && !empty($phone) && !empty($catagory) && !empty($price))
{
$q = mysql_query("INSERT into allads VALUES(NULL,'$title','$phone','$description', '$price','$district','$catagory')");
if($q)
{
echo "<span style='background-color: deepskyblue;'>Your advert is now online!</span>";
}
else
{
echo "Could not update DB";
}
}
}
?>
<html>
<head>
<title>emarket.com</title>
<link rel="stylesheet" type="text/css" href="./style.css"/>
</head>
<header>
<h1>emarket.com</h1>
<h1>Post Ad</h1>
</header>
<body>
<form class="postad" method="post" action="">
<fieldset><caption><h2>Post New Advert</h2></caption></fieldset>
<fieldset>
<label>Title</label><br/>
<input type="text" name="title" class="form-text" required>
</fieldset>
<fieldset>
<label>Description</label><br/>
<input name="description" class="form-text" required>
</fieldset>
<fieldset>
<label>District</label><br/>
<select name="district"required>
<option selected>Colombo</option>
<option>Kandy</option>
<option>Matara</option>
<option>Galle</option>
</select>
</fieldset>
<fieldset>
<label>Category</label><br/>
<select name="catagory"required>
<option value="Electronics">Electronics</option>
<option value="Property">House and Property</option>
<option value="Auto">Automobiles</option>
<option value="Misc">Miscellaneous</option>
</select>
</fieldset>
<fieldset>
<label>Phone</label><br/>
<input type="tel" name="phone" maxlength="10" class="form-text" required>
</fieldset>
<fieldset>
<label>Price</label><br/>
Rs. <input type="number" name="price" class="form-text" required>
</fieldset>
<fieldset>
<input type="submit" name="submit" value="submit"/>
</fieldset>
</form>
</body>
</html>
?>
Edit :- IN situations like these, it's always better to put your php code before <html>
Also, what's your filename in which all this code exists? Put that filename in the <Form action="filenamehere.php">.
If it still doesn't work, upload it somewhere to let me have a closer look.
Your header tag should be inside the body tag.
<body>
<header>
<h1>emarket.com</h1>
<h1>Post Ad</h1>
</header>
Like #Akshay said, you should be using PDO, however you should be escaping your input using something like mysql_real_escape_string($value) before putting it into the database if you aren't using PDO.
Rather inputing one by one for a class/lessen for a day, I'd like to input 10 forms at once.
The HTML is something like this.
All the form do the same thing, add start_time, finish_time and instructor in a database.
However I am not sure how to do this. And I am not sure if this HTML is correct or not.
Any inputs will be appreciated.
Thanks in advance.
HTML
<?php
$date = "2010-10-08";
?>
<form name="form1" method="post" action="inputform.php">
<!-- form 1 -->
<label for='start_time'>1. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 2 -->
<label for='start_time'>2. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 3 -->
<label for='start_time'>3. Start Time</label>
<input type="text" name="start_time" />
<label for='finish_time'>Finish Time</label>
<input type="text" name="finish_time" />
<label for='instructor'>Instructor</label>
<select name="instructor">
<option value="john">John</option>
<option value="mary">Mary</option>
<option value="jim">Jim</option>
</select>
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
<div style="clear: both;"> </div>
<!-- form 4,5,6,7,8,9,10 -->
<input type="submit" name="submit" value="Submit" />
</form>
you cannot submit multiple forms at once.
only one form with the form specific method/action.
however what probably need are arrays.
you can do something like
<form ...>
<select name="instructor[]">
...
</select>
<select name="instructor[]">
...
</select>
then you will get an array posted which you can loop through.
just look at the structure by printing out $_POST like print_r($_POST); then you'll see