Can i allow an empty date field in a form? - php

I have a Form with in php. I have it talking and inserting to the the MYsql database perfectly if i have all fields in the form filled in. Basically i have 3 text fields and 2 dates fields. i only want to chose either date field instead of both.
I have tried only inserting 1 date and it does not insert in to DB only when i have both dates chosen it works.
Below is the code which works when both date fields are chosen. i want to be able to only have 1 or the other or both if possible
<?php
require('db.php');
//Inserting Data Variables
if(isset($_POST['submit']))
{
echo $owner = $_POST['owner'];
echo $budget = $_POST['budget'];
echo $status = $_POST['status'];
echo $s_jan = $_POST['s_jan'];
echo $s_feb = $_POST['s_feb'];
//Query the database
$query = "insert into sbook(owner,budget,status,s_jan,s_feb)
values('$owner','$budget','$status','$s_jan','$s_feb')";
$insert_query = mysqli_query($con, $query);
}
?>
Here is the form
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<label>Owner</label>
<br />
<input class="input" type="text" name="owner">
<br />
<label>EST Budget:</label><br />
<input class="input" type="text" name="budget"/>
<br />
<label>Status:</label><br />
<input class="input" type="text" name="status"/>
<br />
<label>Jan</label><br />
<input class="input" type="date" name="s_jan"/>
<br />
<label>Feb</label><br />
<input class="input" type="date" name="s_feb"/>
<br />
<p>
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Above is the code which works when both date fields are chosen. i want to be able to only have 1 date or the other date or both if possible

Related

How to insert data from a form into a table in php without using databsaes?

I need to create a table of 17 rows where each row contains information such as row number, name, surname, email and birthday. The data is provided by this form:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
After clicking submit the data should be displayed in the nth row of the table(row number one if it is the first "pack" of data submitted, number two if its the second and so on). This problem could easely be solved using databases but i cannot use them(by professors order).
I tried to create an array than push values into it like this:
$array_name = array();
$name = $_POST["name"];
array_push($array_name, $name);
This approach doesn't work(the index of the array stays 0 alla of the time so it keeps replacing the first value again and again) and manually incrementing the index counter of the array doesn't work either.
Normally one should use a database approach but your professor explicitly forbids it.
There are many other ways to do it. (store as TEXT/JSON/CSV file or localstorage / cookies), etc. For me I would use session to do the job
declare a session variable which is an array
if user submits the form, store the POST data into another array (subarray) containing name, surname, birthday, email
add the subarray into the main session variable array
print it out at the end as a table
So the PHP will be:
<?php
session_start();
?>
<form action="#" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="email" placeholder="email" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
<?php
if (!isset($_SESSION["arr"])){
$_SESSION["arr"]=array();
}
if ($_POST) {
$subarray=array(
"name"=>$_POST["name"],
"surname"=>$_POST["surname"],
"birthday"=>$_POST["birthday"],
"email"=>$_POST["email"]
);
$_SESSION["arr"][]=$subarray;
}
echo "<table border=1><tr><td>Name<td>Surname<td>Email<td>Birthday";
foreach($_SESSION["arr"] as $suba){
echo "<tr><td>" . $suba["name"] ;
echo "<td>" . $suba["surname"] ;
echo "<td>" . $suba["email"] ;
echo "<td>" . $suba["birthday"] ;
}
echo "</table>";
?>
However, if you need the data to be persistent (even after the user closes the browser), then you need to store the data say in file format or cookies, etc.
If you need to save data persistent and using file to save data is acceptable, i'd use something like that:
<?php
$file = 'path/to/file.txt';
$data = json_decode(file_get_contents($file), true);
if ($_POST) {
$data[] = [
"name" => $_POST['name'],
"surname" => $_POST['surname'],
"emailbirthday" => $_POST['emailbirthday'],
"birthday" => $_POST['birthday']
];
}
file_put_contents($file, json_encode($data));
?>
<form action="index.php" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Emailbirthday</th>
<th>Birthday</th>
</tr>
<?php
foreach ($data as $row) {
print '<tr>
<td>'.$row['name'].'</td>
<td>'.$row['surname'].'</td>
<td>'.$row['emailbirthday'].'</td>
<td>'.$row['birthday'].'</td>
</tr>';
}
?>
</table>
You can use the post values of hidden fields:
<form action="" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
<?php
if($_POST["names"] == "")
{
$value = $_POST["name"];
}
else
{
$value = $_POST["names"]."-".$_POST["name"];
}
?>
<input type="text" name="names" style='display:none;' value="<?php echo $value ?>">
</form>

$_GET not working for SQL query

I'm trying to use both $_GET and $_POST in an sql query. The following is my code:
<?php
$assignment = mysql_real_escape_string($_GET['name']);
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>
However, the $assignment variable does not work in the query. It is echoed properly before the query is made but its value inside the table after the INSERT is completed is empty. What exactly is causing this?
Instead of trying to combine GET and POST, use a hidden input field:
<?php
$assignment = mysql_real_escape_string($_POST['name']); // Name is now in POST data, so swap this
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
<!-- Add hidden input to carry the name -->
<input type="hidden" name="name" value="<?php echo $_GET['name']; ?>"/>
<!-- Rest of the form is the same -->
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>

showing undefined index while passing variable through url in php

//passing value through url
while($rowcontent=mysqli_fetch_array($details))
{
echo "<tr><td><a href=http://localhost/study/study2/edit.php?toedit=$rowcontent[rollnumber]>edit</a></td><tr>";
}
//receiving value from url
<html>
<form method="GET" action="edit.php">
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
$rollnumber=$_GET["toedit"];
echo $rollnumber;
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
?>
above are two parts of code, where im trying to edit values in a DB by passing value (a roll number) through url but in editing code the value is not being received correctly or some other problem i cant figure out. i did the same for deleting a value from url but it seems to work.
You can use like below:
//receiving value from url
<html>
<form method="GET" action="edit.php">
<?php
while($rowcontent=mysqli_fetch_array($details))
{
?>
<input type="hidden" name="toedit" value="<?php echo $rowcontent[rollnumber]; ?>" />
<?php
}
?>
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
inside edit.php
<?php
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}

Date not showing up when user submits form

So I have a form that users fill out and it adds to a database. Everything works except for the date. The date shows up as 0000-00-00. Currently, I'm using a datepicker, but the date wasn't showing up even when the user had to type it in...so that shouldn't be the issue.
<form name="Add" id="Add" method="post" action="programadd.php">
<p>Program Name:
<input name="program" type="text" id="program" />
</p>
<p>Air Date
<input name="airdate" type="text" id="airdate" />
</p>
<p>Description
<input name="description" type="text" id="description" s />
</p>
<p>Production
<input name="production" type="text" id="production" />
</p>
<p>Promotions
<input name="promotion" type="text" id="promotion" />
</p>
<p>Community
<input name="community" type="text" id="community" />
</p>
<p>Web
<input name="web" type="text" id="web" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
And here's the code that adds it to the database:
<?php require_once('connect-db.php');
$program = $_POST['program'];
$airdate = $_POST['airdate'];
$description = $_POST['description'];
$production = $_POST['production'];
$promotion = $_POST['promotion'];
$community = $_POST['community'];
$web = $_POST['web'];
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, community, promotion, web) VALUES
('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />";
}
else {
die(mysql_error());
}
require_once("db_connx_close.php");
?>
One other question, how do I get the date to display as Month/Date/Year, instead of the reverse?
$date = date('Y-m-d', strtotime($_POST['airdate']));

Using Checkbox Values in PHP

I have this page (code below) which contains a series of 5 checkboxes, all representing different values, and i need to use them to query a database.
<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
<p>Select The <b>Section</b> You Wish To Search In Below... </p>
<p>
<label for="section"></label>
<label for="section2"></label>
<label>
<input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
Functional</label>
<label>
<input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
Technical</label>
<label>
<input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
Commercial</label>
<label>
<input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
Implementation</label>
<label>
<input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
Innovation</label>
<br />
</p>
<p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
<p>
<label for="kword"></label>
<input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
<br />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
</p>
</form>
as you can see each has its own value which is the term used to query the database. the php query code on the results page is as follows
<?php
$kword = $_POST["kword"];
$section = $_POST["SectionSelect"];
function boldText($text, $kword) {
return str_ireplace($kword, "<strong>$kword</strong>", $text);
}
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and section_name like '%$section%'
and keywords like '%$kword%';")
or die(mysql_error());
?>
ultimately what i want this to do is to query the database with the query to potentially have where clauses for each of the checkbox values? for example i want to select x where y like 'Technical' AND y like 'Functional' and so on...
Any help would be great
thanks
Set up your checkbox names as array-keys to use them as an array with values of all selected checkboxes:
<input type="checkbox" name="SectionSelect[0]" value="Functional" />
<input type="checkbox" name="SectionSelect[1]" value="Technical" />
echo $_POST['SectionSelect'][0]; // should print "Functional"
echo $_POST['SectionSelect'][1]; // should print "Technical"

Categories