css for dynamic rows not working - php

<html>
<head>
<title>truck page</title>
<style type="text/css">
tr {
padding: 4px;
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$db = new mysqli("localhost", "root", "", "db1");
function getdata($form_element_name) {
$var = strip_tags($_GET[$form_element_name]);
return $var;
}
?>
<fieldset>
<legend> add a new truck </legend>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
<label>truck number <input type="text" name="truck_number" /></label>
<label>owner name <input type="text" name="truck_owner_name" /></label>
<label>owner phone <input type="text" name="truck_owner_ph" maxlength="10" /></label>
<input type="submit" name="truck_add" value="add" />
<input type="reset" />
</form>
<?php if(isset($_GET['truck_add'])) {
$truck_number = getdata('truck_number');
$truck_owner_name = getdata('truck_owner_name');
$truck_owner_ph = getdata('truck_owner_ph');
$sql_truck_add = "INSERT INTO truck (truck_number, truck_owner_name, truck_owner_ph)
VALUES ('$truck_number', '$truck_owner_name', '$truck_owner_ph' )";
$result_truck_add = $db->query($sql_truck_add);
header("Location: http://localhost/bkp/truckpage.php");
} ?>
</fieldset>
<table>
<tr>
<th>truck number</th>
<th>owner name</th>
<th>phone</th>
<th>operation</th>
<th></th>
</tr>
<?php
$sql_truck_retrieve = "SELECT * from truck";
$result_truck_retrieve= $db->query($sql_truck_retrieve);
while($row=$result_truck_retrieve->fetch_assoc()) {
$truck_num_current = $row['truck_number']; ?>
<tr id="<?php echo "{$truck_num_current}"; ?>">
<td><?php echo "{$row['truck_number']}"; ?></td>
<td><?php echo "{$row['truck_owner_name']}"; ?></td>
<td><?php echo "{$row['truck_owner_ph']}"; ?></td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_numcur" value="<?php echo "{$row['truck_number']}"; ?>" />
<input type="submit" name="truck_update" value="update" />
<input type="submit" name="truck_delete" value="delete" />
</form>
</td>
<td>
<?php // action to be taken if either update or delete is selected
if(isset($_GET['truck_update'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) { ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_update_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="text" name="truck_num_update" placeholder="truck number" />
<input type="text" name="owner_name_update" placeholder="owner name" />
<input type="text" name="owner_phone_update" placeholder="owner phone" />
<input type="submit" value="save" name="truck_update_yes" />
<input type="submit" value="cancel" name="truck_update_no" />
</form>
<?php }
}
else {
if(isset($_GET['truck_delete'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<span>confirm? </span>
<input type="hidden" name="truck_del_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="submit" name="truck_delete_yes" value="yes" />
<input type="submit" name="truck_delete_no" value="no" />
</form>
<?php }
}
}
//
// action to be taken after delete confirmation
if(isset($_GET['truck_delete_yes'])) {
$truck_del_num = $_GET['truck_del_num'];
$sql_truck_del = "DELETE from truck WHERE truck_number = '{$truck_del_num}'";
$result_truck_del = $db->query($sql_truck_del);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_delete_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
//---------------------------------------------------------------------------
//header("Location: http://localhost/bkp/truckpage.php/#$truck_num_current");
//---------------------------------------------------------------------------
}
}
//
// action to be taken after update confirmation
if(isset($_GET['truck_update_yes'])) {
$truck_update_num = $_GET['truck_update_num'];
$truck_num_update = getdata('truck_num_update');
$owner_name_update = getdata('owner_name_update');
$owner_phone_update = getdata('owner_phone_update');
$sql_truck_update = "UPDATE truck SET truck_number = '$truck_num_update', truck_owner_name = '$owner_name_update', truck_owner_ph = '$owner_phone_update' WHERE truck_number = '{$truck_update_num}' ";
$result_truck_update = $db->query($sql_truck_update);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_update_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
}
}
//
?>
</td>
</tr>
<?php } ?>
</table>
</body>
I am trying to extract values from a db table and add them as table rows. The rows are being shown correctly but when I tried to add css styles for the table rows it did not work. I tried to give a border and padding.I am new to web development and cannot understand why the css part is not working.Can anyone please tell how to make the css part work in this php code?

The problem lies in that you cannot very effectively style tr elements. You will need to target the parent table or the child td elements to get the affect you're looking for.
Check out this JSFiddle example: http://jsfiddle.net/v6zNt/
CSS:
table {
border: 1px solid #000;
}
/* notice how these styles does not apply anywhere */
tr {
border: 1px solid green;
padding: 4px;
}
td {
border: 1px solid red;
padding: 4px;
}
HTML:
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
You can style tables, it's just that they have their own set of rules that you need to be mindful of. Here's a great introduction: http://css-tricks.com/complete-guide-table-element/

Related

How to add a CSS class on HTML button via PHP

I have registration form and I need to hide submit button. This button will be visible after action in PHP.
I tried to use CSS classes and it works, but I don't know how to add a class with PHP.
Here is button with class I want to change:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class=I don't know what /><br><br>
</form>
My CSS:
#button8{
visibility: hidden;
font-family: century gothic;
color: #0099FF;
border: 2px solid #0099FF;
background: transparent;
}
#button8.visible{
visibility: visible;}
I need to change to button8.visible but in PHP (I have condition depend on MySQL data).
EDIT:
I need it because, the button will be visible based on code. User put the code, press a button. This action will run PHP: connect to mySQL find if this code is in database. If yes I need my button to be visible. Because of my low knowledge, I want to use if condition in the same PHP session where is mySQL. Here is the whole code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Panákovač 4.0</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
</head>
<body>
</body>
</html>
<?php
// show potential errors / feedback (from registration object)
if (isset($registration)) {
if ($registration->errors) {
foreach ($registration->errors as $error) {
echo $error;
}
}
if ($registration->messages) {
foreach ($registration->messages as $message) {
echo $message;
}
}
}
?>
<!-- register form -->
<form method="post" action="register.php" name="registerform">
<input id="login_input_username" class="login_input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" placeholder="uživatelské jméno" required /><br>
<input id="login_input_password_new" class="login_input" type="password" name="user_password_new" pattern=".{6,}" placeholder="heslo (min. 6 znaků)" required autocomplete="off" /><br>
<input id="login_input_password_repeat" class="login_input" type="password" name="user_password_repeat" pattern=".{6,}" placeholder="zopakujte heslo" required autocomplete="off" /><br>
<input id="login_input_firstname" class="login_input" type="text" name="user_firstname" placeholder="jméno" required /><br>
<input id="login_input_lastname" class="login_input" type="text" name="user_lastname" placeholder="příjmení" required /><br>
<input id="login_input_email" class="login_input" type="email" name="user_email" placeholder="e-mail" required /><br>
<input id="login_input_company" class="login_input" type="text" name="user_company" placeholder="firma" required /><br>
<input type="submit" name="register" value="registrovat" id="button8" class= /><br><br>
</form>
<form method="post" action="register.php" name="registerform">
<input id="login_input_code" class="login_input" type="text" name="user_code" placeholder="ověřovací kód" required /><br>
<input type="submit" name="kod" value="ověřit kód"/><br>
</form>
<?php
if(isset($_POST['kod'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_code = $_POST['user_code'];
$sql = "SELECT `used`,`code` FROM `regcode` WHERE `code` LIKE '$user_code' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "kód: " . $row["code"]. " - použito: " . $row["used"]. $zob . "<br>";
$pouzito = $row["used"];
MY CONDION FOR CLASSES
}
} else {
echo "chybný kód" . $user_code;
}
$conn->close();
}
?>
You can add a php if condition inside the html content like this:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8"
class="<?php if(condition) { echo 'classname'; } ?>" />
</form>
In this case if the condition is true it will echo the class inside the class attribute, there are other ways to do it but this is the most intuitive way for new PHP developers.
Like #Jordi Nebot told you can just do the short version like:
<input class="<?= ($condition) ? 'classname' : ''"> ?>
only way in PHP is to echo the output
echo '<input type="submit" name="register" value="registrovat" id="button8"
class="class1">';
echo '<input type="submit" name="register" value="registrovat" id="button8"
class="class2">';
etc...
or
$class = "class1";
?>
input type="submit" name="register" value="registrovat" id="button8"
class="<?php echo $class; ?>">
<?php
$class = "class2";
?>
input type="submit" name="register" value="registrovat" id="button8"
class="<?php echo $class; ?>">
<?php
<?php $classname = condition ? 'class8' : 'defaultClass'; ?>
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class="<?php echo $classname; ?>" /><br><br>
</form>
You can use ternary operator like:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class"<?= $condition ? 'classvisible' : 'classhidden'"?> />
</form>
I got it. I didn't know, that PHP has to be placed above HTML if i want to use variable from PHP.
IN PHP:
...//some condition
If ($row["used"] == 0){
$myclass ="zobrazeno";
}
...
In HTML
<input type="submit" name="register" value="registrovat" id="button8" class=<?php echo $myclass; ?> /><br><br>
In CSS:
#button8{
visibility: hidden;
font-family: century gothic;
color: #0099FF;
border: 2px solid #0099FF;
background: transparent;
#button8.zobrazeno{
visibility: visible;

value $_POST from others page can't pass throught isset form

I have a problem on pass value $result from different page into another page.
when I POST the value $result into this page. It works fine and I echo it out.
but if i try to pass this $result into isset() form and post it back to self.
The value will become Undefined index.
PAGE1
<?PHP
$connect=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('survey', $connect);
$SQL="SELECT * FROM question";
$data=mysql_query($SQL);
$num=mysql_num_rows($data);
$start=0;
?>
<style type="text/css">
#survey{
width:100%; height:50px;
background:#e6e6e6; border-bottom:solid 1px white;
}
#survey form{
position:relative;
left:320px; top:15px;
float:left;
}
#survey form a{
text-decoration:none;
color:black;
}
.submit{
background-color: transparent;
border: none;
cursor: pointer;
}
</style>
<html>
<div id="container">
<?PHP
if($num<3){
echo "
<div id='create'><a href='question.php'>+ Create a Survey</a></div>
";
}
?>
<?PHP
while($start<$num){
$result=mysql_result($data, $start, 'ID');
$list_num=$start+1;
$ID=$result['ID'];
$list="
<div id='survey'>
<span style='font-size:24px; color:gray; position:relative; float:left; left:10px; top:13px;'>
SURVEY 0$list_num
</span>
<form action='survey.php' method='POST'>
<input class='submit' type='submit' name='submit1' value='View' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='update.php' method='POST'>
<input class='submit' type='submit' name='submit2' value='Edit' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='delete.php' method='POST'>
<input class='submit' type='submit' name='submit3' value='&#10005 Delete' />
<input type='hidden' name='result' value='$ID'/>
</form>
</div>
";
echo $list;
$start++;
}
?>
</div>
</html>
PAGE2
<?PHP
$result=$_POST['result'];
$error="";
if(isset($_POST['submit'])){
$question=$_POST['question'];
$answer_a=$_POST['a'];
$answer_b=$_POST['b'];
$answer_c=$_POST['c'];
if($question && $answer_a && $answer_b && $answer_c){
$connect=mysql_connect("localhost", "root", "");
$db=mysql_select_db("survey", $connect) or die("");
$SQL="UPDATE question SET question='$question', answer_a='$answer_a', answer_b='$answer_b', answer_c='$answer_c'
WHERE ID='$result'";
mysql_query($SQL);
$SQL="UPDATE answer SET question='$question', answer_a='0', answer_b='0', answer_c='0'
WHERE ID='$result' ";
mysql_query($SQL);
header("location:create.php");
}
else{$error="<div style='color:white; background:gray; width:200px; height:20px; text-align:center; line-height:20px;'>"
. "Please fill all the field." . "</div>";}
}
?>
<style>
body{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
</style>
<html>
<form action="update.php" method="POST">
Question<br /><input type="text" name="question" placeholder=" Enter your question here" maxlength="50" /><br/><br/>Answer<br/>
a<input type="text" name="a" placeholder=" Answer a" maxlength="50"/>
b<input type="text" name="b" placeholder=" Answer b" maxlength="50"/>
c<input type="text" name="c" placeholder=" Answer c" maxlength="50"/>
<input type="submit" name="submit" value="SAVE"/>
</form>
<?PHP echo $error; ?>
</html>
Hi All thank you very much for help
I have found the solution
I think the problem is when I pass the value to 2nd form, I will need to resend the value again. Here is what I did.
1st one is from previous page, 2nd one is form post to itself
if(isset($_POST['result'])){
$result=$_POST['result'];
echo $result;
}
if(isset($_POST['data'])){
$ID=$_POST['data'];
echo $ID;
}
add another hidden in the form and resend the value
<input type="hidden" name="data" value="<?PHP echo $result;?>"/>
change the mysql value
WHERE... ID='$ID'";
Try editing on page 2 this
<html>
<form action="update.php" method="POST">
To this
<html>
<form action="" method="POST">

Multiple form submission 1 submit javascript half working

I have 2 forms in 1 page that need submitting i seem to be limited because it is for a hotspot by mikrotik.
The login.html in mikrotic router is
<html>
<head><title>...</title></head>
<body>
$(if chap-id)
<noscript>
<center><b>JavaScript required. Enable JavaScript to continue.</b></center>
</noscript>
$(endif)
<center>If you are not redirected in a few seconds, click 'continue' below<br>
<form name="redirect" action="http://domain.name/login.php" method="post">
<input type="hidden" name="mac" value="$(mac)">
<input type="hidden" name="ip" value="$(ip)">
<input type="hidden" name="username" value="$(username)">
<input type="hidden" name="link-login" value="$(link-login)">
<input type="hidden" name="link-orig" value="$(link-orig)">
<input type="hidden" name="error" value="$(error)">
<input type="hidden" name="chap-id" value="$(chap-id)">
<input type="hidden" name="chap-challenge" value="$(chap-challenge)">
<input type="hidden" name="link-login-only" value="$(link-login-only)">
<input type="hidden" name="link-orig-esc" value="$(link-orig-esc)">
<input type="hidden" name="mac-esc" value="$(mac-esc)">
<input type="submit" value="continue">
</form>
<script language="JavaScript">
<!--
document.redirect.submit();
//-->
</script></center>
</body>
</html>
so then my login.php is this
<?php
$mac=$_POST['mac'];
$ip=$_POST['ip'];
$username=$_POST['username'];
$linklogin=$_POST['link-login'];
$linkorig=$_POST['link-orig'];
$error=$_POST['error'];
$chapid=$_POST['chap-id'];
$chapchallenge=$_POST['chap-challenge'];
$linkloginonly=$_POST['link-login-only'];
$linkorigesc=$_POST['link-orig-esc'];
$macesc=$_POST['mac-esc'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>mikrotik hotspot > login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<style type="text/css">
body {color: #737373; font-size: 10px; font-family: verdana;}
textarea,input,select {
background-color: #FDFBFB;
border: 1px solid #BBBBBB;
padding: 2px;
margin: 1px;
font-size: 14px;
color: #808080;
}
a, a:link, a:visited, a:active { color: #AAAAAA; text-decoration: none; font-size:
10px; }
a:hover { border-bottom: 1px dotted #c1c1c1; color: #AAAAAA; }
img {border: none;}
td { font-size: 14px; color: #7A7A7A; }
</style>
<script language="JavaScript">
function fnSubmit (){
var form1Content = document.getElementById("form1").innerHTML;
var form2Content = document.getElementById("form2").innerHTML;
var form3Content = document.getElementById("form3").innerHTML;
document.getElementById("toSubmit").innerHTML=form1Content+form2Content;
document.forms.toSubmit.submit();
}
</script>
</head>
<body>
<!-- $(if chap-id) -->
<form name="sendin" action="<?php echo $linkloginonly; ?>" method="post" id="form1">
<input type="hidden" name="username" />
<input type="hidden" name="password" />
<input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
<input type="hidden" name="popup" value="true" />
</form>
<script type="text/javascript" src="./md5.js"></script>
<script type="text/javascript">
<!--
function doLogin() {
<?php if(strlen($chapid) < 1) echo "return true;\n"; ?>
document.sendin.username.value = document.login.username.value;
document.sendin.password.value = hexMD5('<?php echo $chapid; ?>' + document.login.password.value + '<?php echo $chapchallenge; ?>');
document.sendin.submit();
return false;
}
//-->
</script>
<!-- $(endif) -->
<div align="center">
Hotspot
</div>
<table width="100%" style="margin-top: 10%;">
<tr>
<td align="center" valign="middle">
Please log on to use the mikrotik hotspot service
</div><br />
<table width="240" height="240" style="border: 1px solid #cccccc; padding: 0px;" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="bottom" height="175" colspan="2">
<!-- removed $(if chap-id) $(endif) around OnSubmit -->
<form method="POST" action="signin.php" id="form2">
First Name<p><input type="text" name="firstname" size="30"></p>
Surname<p><input type="text" name="lastname" size="30"></p>
Email address <font color="red"> (required)</font><p><input type="text" name="email" size="30"></p>
<input type="submit" value="submit" onclick="javascript:fnSubmit();"/>
</form>
<form name="login" id="form3 action="<?php echo $linkloginonly; ?>" method="post" onSubmit="return doLogin()" >
<input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
<input type="hidden" name="popup" value="true" />
<table width="100" style="background-color: #ffffff">
<tr><td align="right"></td>
<td><input type="hidden" style="width: 80px" name="username" type="text" value="user2"/></td>
</tr>
<tr><td align="right"></td>
<td><input type="hidden" style="width: 80px" name="password" type="password" value="user2"/></td>
</tr>
<tr><td> </td>
<td><td></td></td></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<!-- $(if error) -->
<br /><div style="color: #FF8080; font-size: 9px"><?php echo $error; ?></div>
<!-- $(endif) -->
</td>
</tr>
</table>
<script type="text/javascript">
<!--
document.login.username.focus();
//-->
</script>
</body>
</html>
as you can see im trying to auto submit the username and password
so that the user only inputs his email address, to do this i want to store the email in
a database
with signin.php
which is this
<?php
ini_set('display_errors', 'On');
// Receiving variables
#$pfw_ip= $_SERVER['REMOTE_ADDR'];
#$firstname = addslashes($_POST['firstname']);
#$lastname = addslashes($_POST['lastname']);
#$email = addslashes($_POST['email']);
#$date = addslashes($_POST['date']);
// Validation
if (! ereg('[A-Za-z0-9_-]+\#[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
if (strlen($email) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
/* adding user information to the userinfo table */
#$pfw_strQuery = "INSERT INTO userinfo (firstname, lastname, email, creationdate) values ".
"('$firstname', '$lastname', '$email', NOW())";
#$pfw_host = "localhost";
#$pfw_user = "dbusername";
#$pfw_pw = "dbpassword";
#$pfw_db = "database";
$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
die('Could not connect: ' . mysql_error());
}
$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
if (!$pfw_db_selected) {
die ('Can not use $pfw_db : ' . mysql_error());
}
//insert new record
$pfw_result = mysql_query($pfw_strQuery);
if (!$pfw_result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($pfw_link);
header('Location: http://www.google.co.uk');
?>
what happens is that the email address is stored in the mysql table
but the form which stores the username and password, does not pass through, and i end up at the login page
what am i missing?
Thanks all
Ok the answer was to eliminate all the javascript in login.php and use a simple form with the signin.php passing through the username and password using pap login. All working and happy a happy bunny

how to used Jquery form with php while loop more than once

I'm try to run a while loop, i also want jquery form to process it so the page doesn't refresh. So far the code work, but it only used the jquery function on the first loop and for the others loops it open new page. I need some help solving this problem. thanks in advance...
(code)
$query = mysql_query("SELECT * FROM linkUP_message WHERE to_mem='$from_mem'
AND to_mem_burn='2' AND new_message='3' ORDER BY id DESC");
$num_rows = mysql_num_rows($query);
if ($num_rows < 1) {
echo '<div id="new_message" class="addlinkuplinks a"> You have no
message at this time. </div>';
}else {
while($row = mysql_fetch_array($query)){
$msg_id = $row['id'];
$msg_to_mem = $row['to_mem'];
$msg_from_mem = $row['from_mem'];
$msg_from_memID = $row['from_memID'];
$msg_subject = $row['subject'];
$msg_content = $row['content'];
$msg_date = $row['date'];
$msg_from_mem_burn = $row['from_mem_burn'];
$msg_to_mem_burn = $row['to_mem_burn'];
$sqlName=mysql_query("SELECT * FROM prociallinkup_members WHERE
email<>'$from_mem' AND email='$msg_from_mem' LIMIT 1")or die ("Sorry we
have a mysql error!");
while($row=mysql_fetch_array($sqlName)){
$msg_from_memAccount=$row["accountType"];
$msg_from_memID=$row["hdw_id"];
$msg_from_memEmail=$row["email"];
$msg_from_memfirstName=$row["firstName"];
$msg_from_memlastName=$row["lastName"];
$msg_from_mem_pic=$row["profile_pic"];
$gender=$row["gender"];}
echo '
<div id="linkUPmsg" class="linkUPmsg">
<table width="100%">
<tr>
<tr><td width="60%"align="left" valign="top"><span style="font-size:
12px; color: #CCCCCC;">Read Date:</span></td>
<td width="40%"align="right" valign="top"><span style="font-size: 12px;
color: #CCCCCC;">'.$msg_date.'</span></td>
</tr>
<table width="100%">
<tr>
<td width="5%" align="left">'.$profile_pic.'</td>
<td width="87%"align="left" ><span style="font-size: 14px; font-weight:
bold; color: #999999;">'.$msg_from_memfirstName.' '.$msg_from_memlastName.'</span>
<div id="requestlinkUP_text">Send you this message!</div>
<div id="requestlinkUP_text"">Subject: <span style="font-size: 12px;
font-weight: bold; color: #0066CC;">'.$msg_subject.'</span></div>
</td>
<div id="readmember_msg"></div>
<td width="3%" align="right"><div id="linkUPmsg_readR"><form id="readForm"
action="read_message.php" class="readform" method="get" enctype="multipart/form-data"
name="acceptlinkUP">
<input name="RfirstName" id="RfirstName" type="hidden"
value="'.$msg_from_memfirstName.'" />
<input name="RlastName" id="RlastName" type="hidden"
value="'.$msg_from_memlastName.'" />
<input name="Remail" id="Remail" type="hidden"
value="'.$msg_from_memEmail.'" />
<input name="Rdate" id="Rdate" type="hidden" value="'.$msg_date.'" />
<input name="Rcontent" id="Rcontent" type="hidden"
value="'.$msg_content.'" />
<input name="Rsubject" id="Rsubject" type="hidden"
value="'.$msg_subject.'" />
<input name="Rmsg_id" id="Rmsg_id" type="hidden" value="'.$msg_id.'" />
<input name="Rto_mem" id="Rto_mem" type="hidden" value="'.$msg_from_mem.'"
/>
<input name="read_linkUPmsg" class="msgAction" type="image"
src="/image/read_mail.png" width="25" height="25"/>
</form></div></a>
</td>
<td width="5%" align="right"><div id="linkUPmsg_burnR"
class="addlinkuplinks"><form id="burnForm_read" action="/burn_message.php"
class="burnForm_read" method="get" enctype="multipart/form-data"
name="burnlinkUP">
<input name="msg_id" type="hidden" value="'.$msg_id.'" />
<input name="to_mem" type="hidden" value="'.$msg_from_mem.'" />
<input name="burn_linkUPmsg" class="msgAction" type="image"
src="/image/burn_sign.png" width="20" height="25"/>
</form></div><div id="burnmember_msg"></div></a>
</td>
</tr>
</table>
</span>
</div>
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript" src="/jquery.form.js"></script>
<script type="text/javascript" src="/jquery.validate.js"></script>
<script type="text/javascript">
$("document").ready(function()
{
$("#burnForm_read").ajaxForm( {
target: "#burnmember_msg",
success: function() {
$("#linkUPmsg_readR").hide();
$("#linkUPmsg_burnR").hide();
}
});
});
</script>
';
}
}
You need to use array for input type names , as;
<input name="Rdate[]" id="Rdate" type="hidden" value="'.$msg_date.'" />
//do this for all your input fields
//and the on php side do
print_r($_POST);

Submitting radio values in a PHP form to a MySQL table (updating value in table)

I am currently building a php form utilising the jQuery iPhone style radios (found at http://devgrow.com/iphone-style-switches/). By default the radio value is set to NO in the MySQL table for every new entry, but I am having difficulty submitting the form.
How will the form be able to tell the current status of the radio button (i.e. is it set to yes or no upon submission)? Also what MySQL code would I need to use to update the value in the table? This is the code I have so far.
PHP form code
<!--- iphone checkbox --->
<script type="text/javascript">
$(document).ready( function(){
$(".cb-enable").click(function(){
var parent = $(this).parents('.switch');
$('.cb-disable',parent).removeClass('selected');
$(this).addClass('selected');
$('.checkbox',parent).attr('checked', true);
});
$(".cb-disable").click(function(){
var parent = $(this).parents('.switch');
$('.cb-enable',parent).removeClass('selected');
$(this).addClass('selected');
$('.checkbox',parent).attr('checked', false);
});
});
</script>
<style type="text/css">
.cb-enable, .cb-disable, .cb-enable span, .cb-disable span { background: url(resources/switch.gif) repeat-x; display: block; float: left; }
.cb-enable span, .cb-disable span { line-height: 30px; display: block; background-repeat: no-repeat; font-weight: bold; }
.cb-enable span { background-position: left -90px; padding: 0 10px; }
.cb-disable span { background-position: right -180px;padding: 0 10px; }
.cb-disable.selected { background-position: 0 -30px; }
.cb-disable.selected span { background-position: right -210px; color: #fff; }
.cb-enable.selected { background-position: 0 -60px; }
.cb-enable.selected span { background-position: left -150px; color: #fff; }
.switch label { cursor: pointer; }
.switch input { display: none; }
</style>
</head>
<body style="text-align:left;">
<div style="padding: 15px;">
<span class="loginfail" style="font-size:24px; font-weight: bold">Notifications</span><p>
<?php include("progress_insertcomment.php"); ?>
<?php
// Make a MySQL Connection
mysql_select_db("speedycm_data") or die(mysql_error());
$query_comment = "select * from tbl_alert order by id desc limit 1";
$comment = mysql_query($query_comment, $speedycms) or die(mysql_error());
$row_comment = mysql_fetch_assoc($comment);
$totalRows_comment = mysql_num_rows($comment);
?>
<!--- add notification --->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<span id="sprytextarea1">
<textarea id='comment' name="comment" style="height: 75px; width:330px;"><?php echo $row_comment['comment']; ?></textarea>
</span>
<p>
<button type="submit">Add</button>
<input type="hidden" name="notc" value="1"/>
</form>
<!--- notification history --->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="0" cellspacing="2" cellpadding="2">
<?php
if ( $row_comment == 0 ) {
echo "<span style='font-size: 11px;'>No current alerts.</span>";
} else {
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_alert ORDER BY id DESC")
or die(mysql_error());
while($rows=mysql_fetch_array($result)){ ?>
<tr>
<td>
<?php
echo "<div class='bubble'><div class='pimped'>
<blockquote>" . $rows['comment'] . "
</blockquote></div>
<cite><strong>" . $rows['user'] . "</strong> # " . $rows['date'] . "</cite>
<span style='font-size: 10px;'>
<p>
<a href='editalert.php?id=". $rows['id'] ."' class='form' >Edit</a> • <a href='deletealert.php?id=". $rows['id'] ."' class='form'>Delete</a>
</span>
</div>
";
?>
</td>
<td valign="top" align="center"><div style="padding-left: 30px;"><span style="font-size: 10px;">Completed?</span>
<p class="field switch">
<!--- determine status of notification --->
<?php
$status = $rows['status'];
if ( $status == yes ) {
echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="no" checked/>
<input type="radio" id="radio2" name="'.$rows['id'].'" value="yes"/>
<label for="radio1" class="cb-enable selected"><span>Yes</span></label>
<label for="radio2" class="cb-disable"><span>No</span></label>';
} else {
echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="yes"/>
<input type="radio" id="radio2" name="'.$rows['id'].'" value="no" checked/>
<label for="radio1" class="cb-enable"><span>Yes</span></label>
<label for="radio2" class="cb-disable selected"><span>No</span></label>';
}
?>
</p>
</div></td>
</tr>
<tr>
<td></td>
<td align="center"><div style="padding-left: 30px;">
<button type="submit">Update</button>
<input type="hidden" name="notc2" value="1"/>
</div></td>
</tr>
<?php
}
}
?>
</table>
</form>
</div>
code to submit form
<?php
// 6) update notifications
if (array_key_exists('notc2',$_POST)) {
echo "<p style='font-size: 12px;'>Thank you. The notifications have been updated successfully.</p>";
echo "<span style='font-size: 12px;'>
<a onClick=\"history.go(-1)\" class='form'>Return</a>
<p></span>
";
exit;
};
?>
The information will be present within $_POST['<id>'] where <id> corresponds to the row in tbl_alerts as given in your php form:
echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="no" checked/>
<input type="radio" id="radio2" name="'.$rows['id'].'" value="yes"/>
(emphasis added)
You can (and should) verify that by adding a var_dump($_POST) up the top of your processing script.
It will be hard for your processing script to know the id value though so what you probably want is name the radio as some other name, and then add a hidden form field that will store the id.
ex. your radio buttons would appear:
<input type="radio" id="radio1" name="status" value="no" checked/>
<input type="radio" id="radio2" name="status" value="yes"/>
and then somewhere in your code you have:
echo '<input type="hidden" name="statusid" value="'.$rows['id'].'"/>';
That way, the radio button value will be in $_POST['status'] and the id of the row will be in $_POST['statusid']

Categories