/    Sign up×
Community /Pin to ProfileBookmark

Save form value to database

Hi Guys..

Basically i have table contain dates at the top and name on side and i want to save their start time(listbox) and finish time into database according to that name and the date..i tired by giving id but now i dont know how to obtain selected time…? ?

my code below

[code=php]<table border=”1px” id=”shift”>
<tr>
<td>Names</td>

<?php

$ts = strtotime($thisweek);
// find the year (ISO-8601 year number) and the current week
$year = date(‘o’, $ts);

echo “<form action=” method=’GET’>”;
echo “<select name=’week’>”;
for ($i=1;$i<53;$i++)
{
if($i<10)
{
$i=”0″.$i;
}
echo “<option value=’$i’>”;
echo $i;
echo “</option>”;
}
echo “</select>”;
echo “<input type=’submit’ value=’submit’/>”;
echo “</form>”;

if(isset($_GET[‘week’]))
{
$week=$_GET[‘week’];
}
else
{
$week = date(‘W’);
}
// print week for the current date
for($i = 1; $i <= 7; $i++) {
// timestamp from ISO week date format
$ts = strtotime($year.’W’.$week.$i);

echo “<td id='”.date(“Y-m-d”, $ts).”‘>”.date(“d-m-Y “, $ts) . “</td>”;//adding value to TD
}
?>
</tr>

<?php

$query=mysql_query(“SELECT * FROM shift”);
echo “<form action=” method=’GET’ >”;
while($resullt=mysql_fetch_assoc($query))
{
$dbName= $resullt[‘Name’];
$dbDate=$resullt[‘Date’];
$dbID=$dbName.” “.$dbDate;

echo “<tr>”;
echo “<td id=’$resullt[Name]’>$resullt[Name]</td>”;
for($i=1;$i<8;$i++)

{
$ts = strtotime($year.’W’.$week.$i);

// id='”.$resullt[‘Name’].” “.date(“Y-m-d”, $ts).”‘
echo “<td >”;

echo “Start:<select name=’Start’ id='”.$resullt[‘Name’].” “.date(“Y-m-d”, $ts).”‘>”;
echo “<option value=’12’>12</option>”;
echo “<option value=’11’>11</option>”;
echo “<option value=’10’>10</option>”;
echo “<option value=’9′>9</option>”;

echo “</select>”;
echo “Finish:<select name=’Finish’ id='”.$resullt[‘Name’].” “.date(“Y-m-d”, $ts).”‘>”;
echo “<option value=’9′>9</option>”;
echo “<option value=’8′>8</option>”;
echo “<option value=’7′>7</option>”;
echo “<option value=’7′>6</option>”;

echo “</select>”;

echo”</td>”;
}

echo “</tr>”;

}
echo “<input type=’submit’ value=’Save Shift’/>”;
echo “</form>”;
?>

<tr>
<td></td>
<td></td>
</tr>
</table>[/code]

please help or suggest on how can i archive thanks,

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindMar 01.2012 — Step 1. Give each form a unique name

Step 2. When a form is submitted, look at your $_POST["submit"] var, to determine which form you are working with ('submit' or 'Save Shift'). If the second form was submitted, you should have $_POST["Start"] and $_POST["Finish"]. Those would be the start ('9,10,11,12) and stop (6,7,8,9) values.

I would also suggest adding some hidden fields to the form, to include the Name, or some other way to identify WHICH record this is for.

From there, it's just a matter of assembling your SQL insert statement with the available _POST vars.

Give that a try, and post what you come up with, and we'll go from there.
Copy linkTweet thisAlerts:
@jag420authorMar 01.2012 — Thank you for reply...i have tried giving ID to each form for example "Staff1 12-2-2" and using DOM i can get selected value example 12:00 start time however there after i dont no how to run query that will match name and date from the ID and value
Copy linkTweet thisAlerts:
@jag420authorMar 02.2012 — [CODE]<script type='text/javascript'>

function getInfo()
{
var inputs, indexx;

inputs = document.getElementsByTagName('select');
for (indexx = 0; indexx < inputs.length; ++indexx)
{

var getID=document.getElementsByTagName('select')[indexx].id;
var getStart=document.getElementsByTagName('select')[indexx].value;

var mySplitResult = getID.split(" ");
var name=mySplitResult[0];
var wdate=mySplitResult[1];



<?php mysql_query("INSERT INTO shift(Name,Date,Start)VALUES ('".name."','".wdate."','".getStart."') WHERE Name='".name."' AND Date='".wdate."'");?>

console.log(mySplitResult+" "+getStart);
}//end of for
}
</script>
[/CODE]


i have manged to get name date and value but it doesnt insert value in database
Copy linkTweet thisAlerts:
@OctoberWindMar 02.2012 — Because your using JavaScript variables in pop; it doesn't work that way. That's why I had suggested the hidden form fields, and checking on post back.
Copy linkTweet thisAlerts:
@jag420authorMar 02.2012 — but if i use hidden field and try to get it...its going to clash woth date supoose i hve field call date but when i use php $_post[date]...its gona get me all t.he date bit i jus want date and time specific person
Copy linkTweet thisAlerts:
@OctoberWindMar 02.2012 — now that i've taken a second look at this and played around with the code a little, you need to better identify your table rows.

form vars ad identified by their NAME (not ID) in the http transfer; In addition, you need to identify the db row ID that coincides with the name/date. take a look at this:

I don't know what your database stuff looks like, so I just faked it with an array, and swapped while() for a foreach (the concepts is pretty much the same...)

[code=php]

<?php

function kdo($inVar, $text) {
// this function should work;
// if not, replace the kdo() function calls in this snippet with a print_r($var); statement ...

echo "<pre>---------- ". $text ."n";
echo print_r($inVar);
echo "n----------</pre>n";
}
?>
<table border="1px" id="shift">
<tr>
<td>Names</td>

<?php

kdo($_POST, "_POST");


$ts = strtotime($thisweek);
// find the year (ISO-8601 year number) and the current week
$year = date('o', $ts);

echo "<form action='' method='GET'>";
echo "<select name='week'>";
for ($i=1;$i<53;$i++)
{
if($i<10)
{
$i="0".$i;
}
echo "<option value='$i'>";
echo $i;
echo "</option>";
}
echo "</select>";
echo "<input type='submit' value='submit'/>";
echo "</form>";

if(isset($_GET['week']))
{
$week=$_GET['week'];
}
else
{
$week = date('W');
}
// print week for the current date
for($i = 1; $i <= 7; $i++) {
// timestamp from ISO week date format
// kdo($year, "year");
// kdo($week, "week");
// kdo($i, "i");

$ts = strtotime($year.'W'.$week.$i);
echo "<td id='".date("Y-m-d", $ts)."'>".date("d-m-Y ", $ts) . "</td>";//adding value to TD
}
?>
</tr>

<?php

$tmp_array = array(
array(
"key" => 1,
"Name" => "Jill Doe",
"Date" => "3/1/12"
),
array(
"key" => 2,
"Name" => "Jill Doe",
"Date" => "3/2/12"
),
array(
"key" => 3,
"Name" => "John Doe",
"Date" => "3/1/12"
),
array(
"key" => 4,
"Name" => "John Doe",
"Date" => "3/2/12"
)
);



$query=mysql_query("SELECT * FROM shift");
echo "<form action='' method='POST' >";
//while($resullt=mysql_fetch_assoc($query))
foreach ($tmp_array as $key => $resullt)
{
$dbName= $resullt['Name'];
$dbDate=$resullt['Date'];
$dbID=$dbName." ".$dbDate;

echo "<tr>";
echo "<td id='$resullt[Name]'>$resullt[Name]</td>";
for($i=1;$i<8;$i++)

{
$ts = strtotime($year.'W'.$week.$i);

// id='".$resullt['Name']." ".date("Y-m-d", $ts)."'
echo "<td >";

echo "Start:<select name='Start_". $resullt['key'] ."_". $resullt['Name']." ".date("Y-m-d", $ts) ."' id='".$resullt['Name']." ".date("Y-m-d", $ts)."'>";
echo "<option value='12'>12</option>";
echo "<option value='11'>11</option>";
echo "<option value='10'>10</option>";
echo "<option value='9'>9</option>";

echo "</select>";
echo "Finish:<select name='Finish_". $resullt['key'] ."_". $resullt['Name']." ".date("Y-m-d", $ts) ."' id='".$resullt['Name']." ".date("Y-m-d", $ts)."'>";
echo "<option value='9'>9</option>";
echo "<option value='8'>8</option>";
echo "<option value='7'>7</option>";
echo "<option value='7'>6</option>";

echo "</select>";

echo"</td>";
}

echo "</tr>";

}
echo "<input type='submit' value='Save Shift'/>";
echo "</form>";
?>

<tr>
<td></td>
<td></td>
</tr>
</table>



[/code]
Copy linkTweet thisAlerts:
@jag420authorMar 03.2012 — Thank you for reply...yes it works i can get all the name date and times. but i how do i split the array and save into the database.

i tried using [code=php] foreach($_POST as $key => $data)
{
echo "Reading from for eacharray: ".$key." ".$data."<br/>";



}
[/code]


this gives me key(id and date) and value (time)..so basically i was trying to split the $getDatata into separate variable for example get $name $date and $time which makes easy when running the mysql query..

for example [code=php]mysql_query("INSERT INTO schedule(ID,DATE,START,FINISH,NAME) VALUES('$ID','$DATE','$START','$FINISH','$FINISH')")[/code]
Copy linkTweet thisAlerts:
@jag420authorMar 03.2012 — [B][U]Field Type Null Key Default Extra[/U][/B]

Sch_ID int(11) NO PRI NULL auto_increment

Date date NO NULL

Start time NO NULL

Finish time NO NULL

Store_ID int(11) NO MUL NULL

Emp_ID int(11) NO MUL NULL

this is my schedule table in database
×

Success!

Help @jag420 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @bahaedd,
tipped: article
amount: 1000 SATS,

tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,
)...