I am new in PHP. I have no experience on Object oriented PHP. I have experience only in Raw PHP.
I am trying to inserted the value of my drop down box to the database. This is my code given here.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT * INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)"
or die (mysql_error());
$result= mysql_query($sql);
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
<form method="post">
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
<br></br>
</form>
<form method="post">
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post" action="">
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<input type="submit" name="submit" value="submit">
</form>
<br><br>
</form>
</html>
I have gotten $_post['roll'],$_post['first_exam'] these error.
You had a lot of mistakes in your code, here is a full working example:
HTMl & PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Divix Help</title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "
INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)
";
//echo $sql;
$result = mysql_query($sql);
if ($result){
echo 'ok doen';
} else {
echo 'dont';
}
}
?>
<form method="post">
<table>
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
</table>
<br></br>
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
As mentioned by others you had too many <form> tags, remember that you only need 1 form tag in order to send multiple fields via POST or GET method.
You had mistakes in SQL (do not put INSERT * INTO, star sign goes only into SELECT statements)
Broken HTML structure, like missing </body>, </html> tags
And last but not least or die (mysql_error()); you put that against execute or connect function of mysql not against strings.
There is only a simple mistake there. Instead you made many , just make one form & add all select into it...
Something like this:
<form method="post">
code here
</form>
There are few things I would like to point out.
Your include statements should always be at the top of your php document, it's a good programming practice.
Use require_once instead of include because it prevents the file from being included more than once in your php document.
Create and use a Database class to perform all your database operations.
Always sanitize your data using real_escape_string function before inserting to prevent SQL injection.
Always close your database connection at the end, it's a good programming practice.
And last but not the least, learn HTML and PHP properly.
Here's the complete code,
<?php
// instead of include, use require_once
require_once('db.php');
// create and use Database class for all your database operations
class Database{
public static function open_connection($host, $username, $password, $dbName){
return new mysqli($host, $username, $password, $dbName);
}
public static function check_connection(){
if(!mysqli_connect_error()){
return true;
}else{
return false;
}
}
public static function execute_query($connection,$query){
return $connection->query($query);
}
public static function close_connection($connection){
$connection->close();
}
}
?>
<?php
if (isset($_POST['submit'])){
$connection = Database::open_connection(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(Database::check_connection()){
// sanitize your input data
$roll = $connection->real_escape_string($_POST['roll']);
$fifth_exam = $connection->real_escape_string($_POST['first_exam']);
$second_exam = $connection->real_escape_string($_POST['second_exam']);
$third_exam = $connection->real_escape_string($_POST['third_exam']);
$fourth_exam = $connection->real_escape_string($_POST['fourth_exam']);
$fifth_exam = $connection->real_escape_string($_POST['fifth_exam']);
// now insert into database
// instead of taking id as NULL, take id as AUTO_INCREMENT
$query = "INSERT INTO table(roll_no, first_exam, second_exam, third_exam, fourth_exam, fifth_exam) VALUES ($roll, '$first_exam', '$second_exam', '$third_exam', '$fourth_exam', '$fifth_exam')";
if(Database::execute_query($connection,$query)){
echo "record is successfully inserted";
}else{
echo "error: record could not be inserted";
}
}else{
echo "database connection failed";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td>Roll No.</td>
<td><input type="text" name="roll" /></td>
</tr>
<tr>
<td>First exam</td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Second exam</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Third exam</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fourth exam</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fifth exam</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// it's a good programming practice to always close database connection at the end
if(isset($connection)){
Database::close_connection($connection);
}
?>
Try this. Make 1 Form.
Change this too.
$sql= "INSERT INTO roll (Roll,1st,2nd,3rd,4th,5th) VALUES ($roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
This is also one problem. Use or die (mysql_error()); in mysql functions, not in statement.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post">
<table>
<tr>
<th>Roll:</th>
<th>First Exam</th>
<th>Second Exam</th>
<th>Third Exam</th>
<th>Fourth Exam</th>
<th>Fifth Exam</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="roll" /></td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td><input type='submit' name='submit' value='submit'></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th) VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
</html>
Screenhot
Related
I have simple for you problem... :-)
I want to insert data into Database that has 2 tables one is users and second is userprofile
here is my db:
As you see i have foreign-key users->id with userprofile->userid
the problem is that when i want to make registration of new user, i can't....
but when i don't have foreign-key then its all ok.
but i need registration with foreign-key her is the register.php code hope you can hell me :-)
and pleas show me the examples of code were i am making the mistake.
<?php
session_start();
if (isset($_SESSION['user']) != "") {
header("Location: home.php");
}
include_once 'dbconnect.php';
if (isset($_POST['btn-signup'])) {
$y = $_POST['yer'];
$m = $_POST['month'];
$d = $_POST['day'];
$dob = $d.'-'.$m.'-'.$y;
$uname = mysql_real_escape_string($_POST['uname']);
$upass = md5(mysql_real_escape_string($_POST['pass']));
$email = mysql_real_escape_string($_POST['email']);
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$country = mysql_real_escape_string($_POST['country']);
$height = mysql_real_escape_string($_POST['height']);
$hair_color = mysql_real_escape_string($_POST['hair_color']);
$eye_color = mysql_real_escape_string($_POST['eye_color']);
$Body_type = mysql_real_escape_string($_POST['Body_type']);
$gend = mysql_real_escape_string($_POST['gend']);
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
mysql_query("INSERT INTO userprofile(gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')")) {
?>
<script>alert('successfully registered '); </script>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php"); ?>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php");
}
else {
?>
<script>alert('error while registering you...');</script>
<?php
}
}
?>
<!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=utf-8" />
<title>Login & Registration System</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td>
<select name="gend">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select></td>
</tr>
<tr>
<td>
<select name="day">
<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>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input class="yer" type="text" name="yer" placeholder=" yer" required />
</td>
</tr>
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><input type="text" name="fname" placeholder="Your firstname" required /></td>
</tr>
<tr>
<td><input type="text" name="lname" placeholder="Your Lastname" required /></td>
</tr>
<tr>
<tr>
<td>
<select name="country">
<option value="Greece-Athens">Greece-Athens</option>
<option value="Italy-Rome">Italy-Rome</option>
<option value="France-Paris">France-Paris</option>
</select>
<select name="hair_color">
<option value="Black">Black</option>
<option value="Blonde">Blonde</option>
<option value="Dark-Brown">Dark Brown</option>
</select>
<select name="eye_color">
<option value="Black">Black</option>
<option value="Blue">Blue</option>
<option value="Green">Green</option>
<option value="Brown">Brown</option>
<option value="Hazel">Hazel</option>
<option value="Gray">Gray</option>
</select>
<select name="Body_type">
<option value="Slim">Slim</option>
<option value="Athletic">Athletic</option>
<option value="Average">Average</option>
<option value="Overweight">Overweight</option>
</select>
<select name="height">
<option value="152">152</option>
<option value="154">154</option>
<option value="156">156</option>
<option value="158">158</option>
<option value="160">160</option>
<option value="162">162</option>
<option value="164">164</option>
<option value="166">166</option>
<option value="168">168</option>
<option value="170">170</option>
<option value="172">172</option>
<option value="172">172</option>
<option value="174">174</option>
<option value="176">176</option>
<option value="178">178</option>
<option value="180">180</option>
<option value="182">182</option>
<option value="184">184</option>
<option value="186">186</option>
<option value="188">188</option>
<option value="190">190</option>
<option value="192">192</option>
<option value="192">192</option>
<option value="194">194</option>
<option value="196">196</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td>Sign In Here</td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Try this instead:
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
mysql_query("INSERT INTO userprofile(
gender,
dateofbirth,
firstname,
Lastname,
height,
eyecolor,
haircolor,
bodytype,
country_town,
email,
userid)
VALUES (
'$gend',
'$dob',
'$fname',
'$lname',
'$height',
'$eye_color',
'$hair_color',
'$Body_type',
'$country',
'$email',
last_insert_id()
)")) {
Noticed the "last_insert_id()" value and the "userid" column name added.
i recommend you just use one table to store information about the user, password and all.
eg:
$sql = "insert into user (username,email,password,acctype) values ('$uname','$email','$password_hash','$acctype')";
$result = mysqli_query($conn,$sql);
then when a user does certian things you can track that using the user id. It means for every action, you need to post the user id as foreign key in that table.
$sql = "select * from bookings WHERE uid ='".$_SESSION['user_id']."' order by id desc limit 15";
$result = mysqli_query($conn,$sql);
then you can select based on session or uid...
OK i find the way
I modified the code like this:
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')")) {
//$sql="select id from users";
$test=mysql_result(mysql_query("SELECT id FROM users LIMIT 1"),0);
if (mysql_query("INSERT INTO userprofile(userid,gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$test','$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')"))
{
?>
<script>alert('successfully registered '); </script>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php"); ?>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php");
} else {
?>
<script>alert('error while registering you...');</script>
<?php
}
}
<!DOCTYPE html>
<html lang="ro">
<head>
<title>MODIFICA</title>
</head>
<body>
<?php
$select1 = isset($_POST['select1']) ? $_POST['select1'] : '';
if($select1 == 'da'){echo 'ok';}
else { echo '!ok'; }
?>
<form method="POST">
<table border="1">
<thead>
<tr>
<th>Nume</th>
<th>Prenume</th>
<th>Adresa</th>
<th>Partid</th>
<th>Consiliul Local</th>
<th>Primar</th>
<th>Consiliul Judetean</th>
<th>Presedinte Consiliul Judetean</th>
<th>Senat</th>
<th>Deputat</th>
<th>Comisia Europarlamentara</th>
<th>Presedinte</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $db->prepare('SELECT nume,prenume,adresa,partid,consiliul_local FROM candidati');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJ)):?>
<tr>
<td><?php echo $row->nume;?></td>
<td><?php echo $row->prenume;?></td>
<td><?php echo $row->adresa;?></td>
<td><?php echo $row->partid;?></td>
<td>
<select name="select1">
<option value=""></option>
<option value="<?php echo $row->consiliul_local;?>">
<?php echo $row->consiliul_local;?>
</option>
</select>
</td>
<td>
<select name="select2">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select3">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select4">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select5">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select6">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select7">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select8">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<button type="submit" name="btn">SUBMIT</button>
</td>
</tr>
<?php endwhile;?>
</tbody>
</table>
</form>
</body>
</html
I just verify if the $select1 variable is set, and if it's the case, then if the value from "option" (line 44), is 'da', then display it.
The problem that I have is that value is not displayed accordingly. Anyone knows why, and what should I change in order to work?
Or, in simple words, for starting coding the page I need, firstly I just filled-in that "option" tag with the value from db (and yes, the value 'da' is presented in 'consiliul_local' field from db), and then I made a simple echo, above the "form", and if the value of the "select" is 'da', then it should display the message 'ok'. But it always displays the message placed on the else branch. I must specify that I must do the whole logic only with php, and not js or anything else.
Here is how my page looks like, and notice that selected 'da' in the dropdown, which is there because of the MySQL fetch (line 35 in pastebin).
http://www.2shared.com/photo/eXzJ7Glo/1_online.html
This is the page source, which says the value stored in db, is correctly inserted into that value attribute of the tag.
http://www.2shared.com/uploadComplete.jsp?sId=CACRC8H4n6GrF0jJ
So I select 'da' from that dropdown, then I press the button, and I expect the 'ok' message to be displayed. Any fix? Thanks so much!
I have this script. When I select the date then I get the data by selecting date, but I want to set this with timestamp
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th> Birtday </th>
<th> Name </th>
<th> Gender </th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
$result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
$result->bindParam(':a', $d1);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
How I set timestamp with this script
Just use date() to echo out today's date as the value of your d1 input field:
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="<?php echo date("m/d/Y"); ?>" />
<input type="submit" value="Search">
</form>
Ya the date - function will give all the information you need. If you havn't worked with it or just in case you are new to PHP, have a look at the documentation for finding the right date parameters.
For example:
date("d.m.Y") //outputs: 13.03.2014
date("n.d.Y") //outputs: 3.13.2014
date("m.d.y") //outputs: 03.13.14
The whole documentation can be found here: http://php.net/manual/en/function.date.php
EDIT::
So if I got you right you are looking for something simular like this:
<select class="Calendar" id="cdate_Month_ID" >
<option value="1" <?=(date('n')==1?'selected':'')?>>Jan</option>
<option value="2" <?=(date('n')==2?'selected':'')?>>Feb</option>
<option value="3" <?=(date('n')==3?'selected':'')?>>Mar</option>
<option value="4" <?=(date('n')==4?'selected':'')?>>Apr</option>
<option value="5" <?=(date('n')==5?'selected':'')?>>May</option>
<option value="6" <?=(date('n')==6?'selected':'')?>>Jun</option>
<option value="7" <?=(date('n')==7?'selected':'')?>>Jul</option>
<option value="8" <?=(date('n')==8?'selected':'')?>>Aug</option>
<option value="9" <?=(date('n')==9?'selected':'')?>>Sep</option>
<option value="10" <?=(date('n')==10?'selected':'')?>>Oct</option>
<option value="11" <?=(date('n')==11?'selected':'')?>>Nov</option>
<option value="12" <?=(date('n')==12?'selected':'')?>>Dec</option>
</select>
<select class="Calendar" id="cdate_Day_ID" >
<option <?=(date('d')==1?'selected':'')?>>1</option>
<option <?=(date('d')==2?'selected':'')?>>2</option>
<option <?=(date('d')==3?'selected':'')?>>3</option>
<option <?=(date('d')==4?'selected':'')?>>4</option>
<option <?=(date('d')==5?'selected':'')?>>5</option>
<option <?=(date('d')==6?'selected':'')?>>6</option>
<option <?=(date('d')==7?'selected':'')?>>7</option>
<option <?=(date('d')==8?'selected':'')?>>8</option>
<option <?=(date('d')==9?'selected':'')?>>9</option>
<option <?=(date('d')==10?'selected':'')?>>10</option>
<option <?=(date('d')==11?'selected':'')?>>11</option>
<option <?=(date('d')==12?'selected':'')?>>12</option>
<option <?=(date('d')==13?'selected':'')?>">13</option>
<option <?=(date('d')==14?'selected':'')?>>14</option>
<option <?=(date('d')==15?'selected':'')?>>15</option>
<option <?=(date('d')==16?'selected':'')?>>16</option>
<option <?=(date('d')==17?'selected':'')?>>17</option>
<option <?=(date('d')==18?'selected':'')?>>18</option>
<option <?=(date('d')==19?'selected':'')?>>19</option>
<option <?=(date('d')==20?'selected':'')?>>20</option>
<option <?=(date('d')==21?'selected':'')?>>21</option>
<option <?=(date('d')==22?'selected':'')?>>22</option>
<option <?=(date('d')==23?'selected':'')?>>23</option>
<option <?=(date('d')==24?'selected':'')?>>24</option>
<option <?=(date('d')==25?'selected':'')?>>25</option>
<option <?=(date('d')==26?'selected':'')?>>26</option>
<option <?=(date('d')==27?'selected':'')?>>27</option>
<option <?=(date('d')==28?'selected':'')?>>28</option>
<option <?=(date('d')==29?'selected':'')?>>29</option>
<option <?=(date('d')==30?'selected':'')?>>30</option>
<option <?=(date('d')==31?'selected':'')?>>31</option>
</select>
<input class="Calendar" type="text" size="4" maxlength="4" title="Year" value="<?=date('Y'?>">
I have a script that I am writing and I have encounted a problem. I can not get the information from a dropbox to submit into a MySQL database.
Everything will show up in the database except for the item from the dropbox.
Here is my code:
<form action="work.php" method="post">
<table>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" tabindex="1" />
</td>
</tr>
<tr>
<td>Messgae:</td>
<td><textarea name="Message"
rows = "7"
cols = "35"
tabindex = "2" /></textarea>
</td>
</tr>
<tr>
<Td>Time</td>
<td>
<select name="hour" tabindex="3">
<option value="1">1AM</option>
<option value="2">2AM</option>
<option value="3">3AM</option>
<option value="4">4AM</option>
<option value="5">5AM</option>
<option value="6">6AM</option>
<option value="7">7AM</option>
<option value="8">8AM</option>
<option value="9">9AM</option>
<option value="10">10AM</option>
<option value="11">11AM</option>
<option value="12">12PM</option>
<option value="13">1PM</option>
<option value="14">2PM</option>
<option value="15">3PM</option>
<option value="16">4PM</option>
<option value="17">5PM</option>
<option value="18">6PM</option>
<option value="19">7PM</option>
<option value="20">8PM</option>
<option value="21">9PM</option>
<option value="22">10PM</option>
<option value="23">11PM</option>
<option value="24">12AM</option>
</select><br>
<input type="submit" name="formSubmit" value="Submit" >
</form>
And then here is work.php
<?php
$time = $_POST['hour'];
$email1 = $_POST['Email'];
$message1 = $_POST['Message'];
mysql_connect ('localhost', 'myuser', 'mypassword') or die ('Error: ' .mysql_error());
mysql_select_db ("mydatabase");
$query="INSERT INTO Reminder (Email, Message, Time)
VALUES
('$email1','$message1','$time')";
mysql_query($query) or die ('Error submitting');
echo "You Chose: " .$time ;
?>
I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.
If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …
Then you can acces the array in your PHP script
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GET may be substituted by $_POST depending on the <form method="…" value.
Change:
<select name="select2" ...
To:
<select name="select2[]" ...
You can use this code to retrieve values from multiple select combo box
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
Use the following program for select the multiple values from select box.
multi.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
This will display the selected values:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
You could do like this too. It worked out for me.
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-)
I hope it'll be work for somebody more. Thanks to all.
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
form submit with enctype="multipart/form-data"