...

View Full Version : Help Posting dynamically created checkboxes adn radio buttons to MySQL



lumper
04-17-2012, 01:00 PM
I need a lot of help here, I don't know anything about getting values from my form into Mysql and vice versa.

Please could someone help me create the statements to get the information from the dynamically created Schedule days and times to the DB and to reflect shoudl a person re-open this page for editing?

here's my form code below:



<?php
//Start session
session_start();
if (!array_key_exists("user", $_SESSION)) {
header('Location: index.php');
exit;
}
//Create a new database object
require_once("Includes/db.php");

//Retrieve the ID of the user who is trying to add a Page
$userID = PageDB::getInstance()->get_user_id_by_name($_SESSION['user']);
//Initialize $pageMessageIsEmpty
$pageMessageIsEmpty = false;


if ($_SERVER['REQUEST_METHOD'] == "POST") {

//Checks whether the a "message" field is empty
if ($_POST['message'] == "") {
$pageMessageIsEmpty = true;
}


if (!isset($_POST['submit']));

if (isset ($_POST['norm']));
if (isset ($_POST['lrg']));
if (isset ($_POST['blod']));
if (isset ($_POST['blod']));
// Checkboxes checked


//The "pageID" is not empty and adds the message into DB
else if ($_POST['pageID'] == "") {
PageDB::getInstance()->insert_page($userID, $_POST['message'] , $_POST['createdDate']);
header('Location: editPageList.php');
exit;
} else if ($_POST['pageID'] != "") {
PageDB::getInstance()->update_page($_POST['pageID'], $_POST['message'] ,$_POST['createdDate']);
header('Location: editPageList.php');
exit;
}
}

function create_time_range($start, $end, $by='60 mins') {

$start_time = strtotime($start);
$end_time = strtotime($end);

$times = array();
for ( ;$start_time < $end_time; ) {
$times[] = $start_time;
$start_time = strtotime('+'.$by, $start_time);
}
$times[] = $start_time;
return $times;
}

// create array of time ranges
$times = create_time_range('0:00', '23:00', '1 hour');


// $times = create_time_range('9:30am', '5:30pm', '30 mins');

// format the unix timestamps
foreach ($times as $key => $time) {
$times[$key] = date('H:i', $time);
}

//print '<pre>'. print_r($times, true).'</pre>';

$days = array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
);

$s_day = array(
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Creator</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="pagelist.css" type="text/css" rel="stylesheet" media="all" />
<link href="Includes/buttons.css" type="text/css" rel="stylesheet" media="all" />

<script type="text/javascript" src="Includes/current_time.js"></script>
<script type="text/javascript" src="Includes/buttons.js"></script>
<!--Fetch Language Value Character Map-->
<script type="text/javascript" src="Includes/eng_chars.js"></script>
<!-- <script type="text/javascript" src="Includes/heb_chars.js"></script>-->
<script type="text/javascript" src="Includes/format_text.js"></script>
<script type="text/javascript" src="Includes/all_tricks.js"></script>

</head>
<body role="application">

<?php
$query_autonow = "INSERT INTO pages (created_date)
VALUE ('TIMESTAMP: Auto NOW()', NOW() )";


if ($_SERVER['REQUEST_METHOD'] == "POST")
$page = array("id" => $_POST['pageID'], "msg_txt" => $_POST['message'], "created_date" => $_POST['createdDate']);
else if (array_key_exists("pageID", $_GET));
else{
$page = array("id" => "", "msg_txt" => "", "created_date" => "");

}
if ($_SERVER['REQUEST_METHOD'] == "POST")
$message = array("idnorm_msg" => $_POST['messID'], "nm_msg_txt" => $_POST['nm_message']);
else if (array_key_exists("pageID", $_GET));
else{
$message = array("idnorm_msg" => "", "nm_msg_txt" => "");

}

?>
<!-- Start the Page Explorer Form -->
<form name="editPage" method="post" id="pageForm">
<table width="997" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="gradient_bg">


<table width="1000" border="0" cellpadding="0" cellspacing="0">
<tr colspan="2">
<td height="20" colspan="2" class="header_bg">&nbsp;</td>
</tr>
<tr colspan="2">
<td width="426" align="left" valign="top"><!-- Start the Page Form -->
<input type="hidden" name="pageID" value="<?php echo $page['id']; ?>" />
<fieldset class="message_grad">
<legend>Message:</legend>
<table width="360" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="223">
<div id="showmessage" style="display:block">
<textarea name="message" id="message" value="<?php echo $page['msg_txt']; ?>" rows="4" cols="24" onkeyup="copy_data(this)"></textarea>
<br/>
<?php
if ($pageMessageIsEmpty)
echo '<div class="error">Please enter message</div>';
?>
<textarea name="messageCode" id="copy_to" value="<?php echo $message['nm_msg_txt']; ?>" readonly="readonly"></textarea>
</div>
</td>
<td width="137" align="left" valign="top">

<table width="139" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="35" align="right"><div id="nm-button">
<label>
<input type="checkbox" value="1" <?php if ($norm) echo 'checked="checked"' ?> id="norm" name="norm" onClick="changenorm()"><span>&nbsp;</span>
</label>
</div></td>
<td width="35"><div id="lg-button">
<label>
<input type="checkbox" value="1" id="lrg" name="lrg" <?php if ($lrg) echo 'checked="checked"' ?> onClick="changelrg()"><span>&nbsp;</span>
</label>
</div></td>
<td width="35"><div id="bd-button">
<label>
<input type="checkbox" value="1" id="blod" name="blod" <?php if ($lrg) echo 'checked="blod"' ?> onClick="changeblod();"><span>&nbsp;</span>
</label>
</div></td>
<td width="31"><div id="bl-button">
<label>
<input type="checkbox" value="1" id="bex" name="bex" <?php if ($bex) echo 'checked="checked"' ?> onClick="changebex()"/>
<span>&nbsp;</span> </label>
</div></td>
</tr>
<tr>
<td align="right"><div id="cn-button">
<label>
<input type="checkbox" value="1"><span>&nbsp;</span>
</label>
</div></td>
<td><div id="fl-button">
<label>
<input type="checkbox" value="1" name="flash" id="flash" <?php if ($lrg) echo 'checked="checked"' ?>/>
<span>&nbsp;</span> </label>
</div>
</td>
<td><div id="tm-button">
<label>
<input type="checkbox" value="1" id="tim" name="tim" <?php if ($tim) echo 'checked="checked"' ?> onclick="changetim()"/>
<span>&nbsp;</span> </label>
</div></td>
<td><div id="tp-button">
<label>
<input type="checkbox" value="1" id="temp" name="temp" <?php if ($temp) echo 'checked="checked"' ?>onClick="changetemp()"/>
<span>&nbsp;</span> </label>
</div></td>
</tr>
<tr>
<td align="right"><div id="rv-button">
<label>
<input type="checkbox" value="1" name="invt" id="invt" <?php if ($invt) echo 'checked="checked"' ?>/>
<span>&nbsp;</span></label>
</div></td>
<td><a href="#" class="char_btn" onclick="return false;">&nbsp;</a></td>
<td><div id="ln-button">
<label>
<input type="checkbox" value="1" />
<span>&nbsp;</span></label>
</div></td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</fieldset>

<br/></td>
<td width="574" align="left" valign="top">
<label for="tempo_DD2">Tempo</label>
<select name="tempo_DD" id="tempo_DD2">
<option value="" selected="selected"></option>
<option value="1">2 Sec</option>
<option value="2">5 Sec</option>
<option value="3">10 Sec</option>
<option value="4">20 Sec</option>
<option value="5">30 Sec</option>
<option value="6">45 Sec</option>
<option value="7">60 Sec</option>
<option value="8">90 Sec</option>
<option value="9">120 Sec</option>
</select>
&nbsp;&nbsp;
<?php
if(isset($_GET['nm_pagefunc']))
{
$query1="SELECT * FROM pages WHER nm_pagefunc='$transDD1'";
$result1=mysql_query($query1);
$x=0;
$nums=mysql_numrows($result1);

while($x<$num)
{
$f1 = mysql_result($result1,$x,1) or die("Failed Query");
echo "name is ".$f1;
$x++;
}
}
?>
<label for="transDD">Transition</label>
<select name="transDD[]" id="trans_dd" >
<option value="0" selected="selected" id="trans_dd" >Auto</option>
<option value="1" id="trans_dd" >Appear</option>
<option value="2" id="trans_dd" >Wipe</option>
<option value="3" id="trans_dd" >Open</option>
<option value="4" id="trans_dd" >Lock</option>
<option value="5" id="trans_dd" >Rotate</option>
<option value="6" id="trans_dd" >Right</option>
<option value="7" id="trans_dd" >Left</option>
<option value="8" id="trans_dd" >Roll Up</option>
<option value="9" id="trans_dd" >Roll Down</option>
<option value="12" id="trans_dd" >Paint</option>
<option value="13" id="trans_dd" >Fade In</option>
<option value="14" id="trans_dd" >Jump</option>
<option value="15" id="trans_dd" >Slide</option>
</select>



&nbsp;&nbsp;

<?php $scheduled = 'unchecked';
if (isset($_POST['nm_scheduled']))
if ($scheduled=='1'){
$scheduled ='checked';
}
else ($scheduled='0');

?>
nm_scheduled
<label for="nm_schedule">Set a Schedule</label>
<input type="checkbox" name="nm_schedule" id="nm_schedule" onClick="schedhow()" />
<br />
<div id="scheduley" style="display:none;">
<?php
// Days Active
$scheduled = 'unchecked';
if (isset($_POST['nm_scheduled']))
if ($scheduled=='1'){
$scheduled ='checked';
}

echo("<fieldset class=\"active_day\"><legend>Days Active:</legend>");

for ($i=0; $i<sizeof($days); $i++){
echo("<div class=\"on_lbl\">
<input type=\"checkbox\" name=\"dayOption\" value=\"" . $i . "\" id=\"onoption" . $i . "\" checked=\"checked\" onClick=\"dissad('$i')\">
<label for=\"option" . $i . "\">" . $days[$i] . "</label>
</div>");

}
echo ("</fieldset>");

// Select Days for Schedule
echo ("<fieldset class=\"sched_day\"><legend>Schedule Times:</legend>");
for ($i=0; $i<sizeof($s_day); $i++)
{

echo("<span id=\"daycell" . $i . "\" style=\"display:table-cell;\">
<input type=\"radio\" name=\"sdayOption\" value=\"" . $i . "\" id=\"dayopt" . $i . " \" onClick=\"disshow('$i')\" >
<label class=\"short\">$s_day[$i]</label></span>");

}
echo("</fieldset>");
// Day: Schedule show Hours
echo "<div id=\"myfield\">";
echo("<fieldset><legend id=\"legend\">day</legend>");

for ($i=0; $i<sizeof($times); $i++)

{
echo("<span id=\"t_option" . $i . "\" style=\"background-image:url(images/ton.png); padding-top:5px; padding-bottom:5px; min-width:120px; width:120px; max-width:120px;\">
<input type=\"checkbox\" name=\"timeOption" . $i . "\" value=\"" . $i . "\" checked=\"checked\" id=\"toption" . $i . "\" onClick=\"changeColour('$i')\" />
<label for=\"timeOption" . $i . "\" style=\"padding-right:2px;\">" . $times[$i] . "</label></span>");
}

echo ("<br><br>");
echo ("<input type=\"button\" value=\"Update\" onClick=\"\">");
echo ("</fieldset>");
echo "</div>";
?>
</div></td>
</tr>
<tr colspan="2">
<td colspan="2" align="left" valign="top">&nbsp;</td>
</tr>
<tr colspan="2">
<td colspan="2" align="left" valign="top">&nbsp;</td>
</tr>
<tr colspan="2">
<td colspan="2" align="center" valign="top" style="background:url(images/td_rad.png) left top repeat-x;"><div id="marquee_hold" style="text-align:left;">
<table border="0" align="left" cellpadding="0" cellspacing="0" >
<tr>
<td align="left" valign="top" style="height:64px; width:302px; background:url(images/led_holder.png) no-repeat center top;padding-left:10px; padding-right:10px;">
<marquee behavior="scroll" direction="left" loop="0" id="ticker" style="display:block;">
</marquee>
</td></tr></table>
</div></td>
</tr>
<tr colspan="2">
<td colspan="2" align="center" valign="top" ><input type="submit" name="savePage" value="Save Changes"/>
<input type="button" name="clear" value="Clear Form" onclick="document.getElementById('pageForm').reset();"/></td>
</tr>
</table></td>
</tr>
</table>
</form>
<!-- End Form -->
</body>
</html>


heres my DB connect code:


<?php

class PageDB extends mysqli {

// single instance of self shared among all instances
private static $instance = null;
// db connection config vars
private $user = "root";
private $pass = "";
private $dbName = "test";
private $dbHost = "localhost";
private $con = null;

//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}

// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}

public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}

// private constructor
private function __construct() {
parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');
}

public function get_user_id_by_name($name) {
$name = $this->real_escape_string($name);
$user = $this->query("SELECT id FROM users WHERE name = '"
. $name . "'");

if ($user->num_rows > 0){
$row = $user->fetch_row();
return $row[0];
} else
return null;
}



public function get_pages_by_user_id($userID) {
return $this->query("SELECT id, msg_txt, created_date FROM pages WHERE user_id=" . $userID);
}
public function create_user($name, $email, $organisation, $password) {
$name = $this->real_escape_string($name);
$email = $this->real_escape_string($email);
$organisation = $this->real_escape_string($organisation);
$password = $this->real_escape_string($password);
$this->query("INSERT INTO users (name, email, organisation, password) VALUES ('" . $name
. "', '" . $email . "', '" . $organisation . "', '" . $password . "')");

}

public function verify_user_credentials($name, $password) {
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$result = $this->query("SELECT 1 FROM users WHERE name = '" . $name . "' AND password = '" . $password . "'");
return $result->data_seek(0);
}

function insert_page($userID,$message, $createdDate) {
$message = $this->real_escape_string($message);
if ($this->format_date_for_sql($createdDate)==null){
$this->query("INSERT INTO pages (user_id, msg_txt, created_date)" .
" VALUES (" . $userID . ", '" . $message . "')");
} else
$this->query("INSERT INTO pages (user_id, msg_txt, created_date)" .
" VALUES (" . $userID . ", '" . $message . "', "
. $this->format_date_for_sql($createdDate) . ")");
}



function format_date_for_sql($date) {
if ($date == "")
return null;
else {
$dateParts = date_parse($date);
return $dateParts['year'] * 10000 + $dateParts['month'] * 100 + $dateParts['day'];
}
}

public function update_page($pageID, $message, $createdDate) {
$message = $this->real_escape_string($message);
$this->query("UPDATE pages SET msg_txt = '" . $message . "', created_date = " . $createdDate . " WHERE id =" . $pageID);
}

public function get_page_by_page_id($pageID) {
return $this->query("SELECT msg_txt, created_date FROM pages WHERE id = " . $pageID);
}


public function delete_page($pageID) {
$this->query("DELETE FROM pages WHERE id = " . $pageID);
}

}

?>


My DB columns are:


id int(11) No None AUTO_INCREMENT
user_id int(11) No None
msg_txt varchar(1024) Yes NULL
created_date datetime Yes NULL
idnorm_msg int(11) Yes NULL
nm_iddisplay int(11) No None
nm_pg_no int(3) No None
nm_msg_txt varchar(1024) No None
nm_monday tinyint(1) No 1
nm_tuesday tinyint(1) No 1
nm_wednesday tinyint(1) No 1
nm_thursday tinyint(1) No 1
nm_friday tinyint(1) No 1
nm_saturday tinyint(1) No 1
nm_sunday tinyint(1) No 1
nm_scheduled tinyint(1) No 0
nm_invert tinyint(1) No 0
nm_centre tinyint(1) No 0
nm_foreignlang tinyint(1) No 0
nm_bold1_2 tinyint(1) No 0
nm_bold3_4 tinyint(1) No 0
nm_bold5_6 tinyint(1) No 0
nm_bold7_8 tinyint(1) No 0
nm_pagefunc varchar(15) No Appear
nm_pagetype varchar(15) No TEXT
nm_pagetempo varchar(15) No 10 sec
nm_starttime datetime No 2012/03/01 00:00
nm_endtime datetime No 2012/03/01 23:59
nm_lastsend datetime No 2012/03/01 00:00
nm_lastmodified datetime No 2012/03/01 23:59


All help is truly appreciated ... my deadline is 12:00 tonight (GMT +2)

angst
04-17-2012, 01:25 PM
holy code dump! just post the relevant code, not the entire pages and you might get more help here...

lumper
04-17-2012, 04:22 PM
My apologies Angst - I am not really aware of exactly what is necessary in these cases ... unfortunately my issue is all in one page, but here's the excerpt of where I am having the problem.




<label for="nm_schedule">Set a Schedule</label>
<input type="checkbox" name="nm_schedule" id="nm_schedule" onClick="schedhow()" />
<br />
<div id="scheduley" style="display:none;">
<?php
// Days Active
$scheduled = 'unchecked';
if (isset($_POST['nm_scheduled']))
if ($scheduled=='1'){
$scheduled ='checked';
}

echo("<fieldset class=\"active_day\"><legend>Days Active:</legend>");

for ($i=0; $i<sizeof($days); $i++){
echo("<div class=\"on_lbl\">
<input type=\"checkbox\" name=\"dayOption\" value=\"" . $i . "\" id=\"onoption" . $i . "\" checked=\"checked\" onClick=\"dissad('$i')\">
<label for=\"option" . $i . "\">" . $days[$i] . "</label>
</div>");

}
echo ("</fieldset>");

// Select Days for Schedule
echo ("<fieldset class=\"sched_day\"><legend>Schedule Times:</legend>");
for ($i=0; $i<sizeof($s_day); $i++)
{

echo("<span id=\"daycell" . $i . "\" style=\"display:table-cell;\">
<input type=\"radio\" name=\"sdayOption\" value=\"" . $i . "\" id=\"dayopt" . $i . " \" onClick=\"disshow('$i')\" >
<label class=\"short\">$s_day[$i]</label></span>");

}
echo("</fieldset>");
// Day: Schedule show Hours
echo "<div id=\"myfield\">";
echo("<fieldset><legend id=\"legend\">day</legend>");

for ($i=0; $i<sizeof($times); $i++)

{
echo("<span id=\"t_option" . $i . "\" style=\"background-image:url(images/ton.png); padding-top:5px; padding-bottom:5px; min-width:120px; width:120px; max-width:120px;\">
<input type=\"checkbox\" name=\"timeOption" . $i . "\" value=\"" . $i . "\" checked=\"checked\" id=\"toption" . $i . "\" onClick=\"changeColour('$i')\" />
<label for=\"timeOption" . $i . "\" style=\"padding-right:2px;\">" . $times[$i] . "</label></span>");
}

echo ("<br><br>");
echo ("<input type=\"button\" value=\"Update\" onClick=\"\">");
echo ("</fieldset>");
echo "</div>";
?>



Again, sorry for the code bump earlier. Thank you for showing me the error of my ways ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum