I'am using php 7.0, apache2, mysql ver 14.14 distribution 5.6.17, and phpmyadmin 5.7
My php file for some reason will not display the value for variable $result. Whenever I try to use "echo" it wont display it. Not even another variable (sum).
My html file has following code:
<!doctype html>
<html lang="en-us">
<head>
<title>calculation form</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="calculate.php">
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>Calculation:<br>
<input type="radio" name="calc" value="add"> add<br>
<input type="radio" name="calc" value="subtract"> subtract<br>
<input type="radio" name="calc" value="multiply"> multiply<br>
<input type="radio" name="calc" value="divide"> divide
</p>
<p><input type="submit" name="submit" value="Calculate"></p>
</form>
</body>
</html>
My php code has the following:
<?php
$sum = 0;
if(($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
}
if($_POST[calc] == "subtract")
{
$result = $_POST[val1] - $_POST[val2];
}
if($_POST[calc] == "multiply")
{
$result = $_POST[val1] - $_POST[val2];
}
if ($_POST[calc] == "divide") {
$result = $_POST[val1] / $_POST[val2];
}
?>
<?php
echo $result;
echo $sum;
?>
you have to echo inside if condition or you have to set variable globally
<?php
$sum = 0;
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$result = $_POST['val1'] + $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "subtract")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "multiply")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if ($_POST['calc'] == "divide") {
$result = $_POST['val1'] / $_POST['val2'];
echo $result;
}
?>
or else set like this
if($_POST[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
$sum= $result;
}
also yuo have error in $_POST[val1]
it should be $_POST['val1']
Updated with example
<?php
$sum = 0;
$_POST['val1']=5;
$_POST['val2']=10;
$_POST['calc']='add';
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$sum = $_POST['val1'] + $_POST['val2'];
}
if($_POST['calc'] == "subtract")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if($_POST['calc'] == "multiply")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if ($_POST['calc'] == "divide") {
$sum = $_POST['val1'] / $_POST['val2'];
}
echo $sum;
?>
Related
My error because the user inputted a non numerical value, the if else statement to change the value of the incrementing value to a positive number if the user inputs a negative value and the change if the start value is greater that the stop value is not working either.
<?php
$banner = "Self-referring Forms w/Data Validation";
include ("./header.php");
$start = "";
$stop = "";
$incr = "";
$error = "";
$output = "";
$checkResult = false;
$MAX_ITERATIONS = 100;
?>
<h2><?php echo $output; ?></h2>
<h3><?php echo $error; ?></h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<p id="para6">Starting Temperature: <input
type="text" name="starting_number" size="12"<?php ?>/><br/>
<p id="para6">Stop Temperature :
<input type="text" name="stop_number" size="12" /><br/>
<p id="para6">Temperature Increment: <input type="text"
name="increment_number" size="12" /><br/></p>
<input id="para6" type="submit" value="Create Temperature Conversion Table" name="calculate"/>
<?php
error_reporting(E_ERROR | E_PARSE);
try
{
if($_SERVER["REQUEST_METHOD"] == "GET"){
$start = "";
$stop = "";
$incr = "";
}
else if($_SERVER["REQUEST_METHOD"] == "POST")
{
$start=(double)trim($_POST["starting_number"]);
$stop=(double)trim($_POST["stop_number"]);
$incr=(double)trim($_POST["increment_number"]);
$num = (double)($stop-$start)/$incr;
if($start == "" || $stop == "" || $incr == "" || $num == "")
{
//means the user did not enter anything
$error .= "The text boxes cannot be blank.";
echo $error;
}
else if($num>$MAX_ITERATIONS)
{
$error .= "The conditions will cause too many iterations (max. 100), therefore (for the
sake of server resources) your request is denied.";
echo $error;
}
This is where I need help
else if($start >= $stop)
{
$start == $stop;
$stop == $start;
}
else if($incr == "0")
{
$error .= "You must enter a non-zero increment.";
echo $error;
}
else if($incr < "0")
{
$incr == $incr * (-1);
}
else if($start == input type="text" || $stop == input type="text" ||$incr == input
type="text" || $num == input type="text")
{
$error .= "You must enter a numerical value";
echo $error;
}
The rest is just for you to see what the rest of my code looks like
else
{
echo "<table border='1'>";
echo "<tr>";
echo "<th>Celcius</th>";
echo "<th>Fahrenheit</th>";
echo "</tr>";
for($i=$start; $i<=$stop; $i += $incr)
{
echo "<tr>";
echo "<td>". $i . "°</td>";
echo "<td>". (($i * 9/5) + 32 ). "°</td>";
echo "</tr>";
}
echo "</table>";
}
if($error == "")
{
$output = "";
}
else
{
$error .= "<br/>Please try again.";
}
}
}
catch(DivisionByZeroError $e)
{
echo "The text boxes cannot be blank.";
}
catch(Exception $e)
{
echo 'Please Enter correctly';
}
?>
</form>
<?php
include ("./footer.php");
?>
This is form for calculator
<form method='post' action='result.php' name='calc_form'>
<input type='text' name='input1' size='15'>
<select name='operation'>
<option value="plus">+</option>
<option value="minus">-</option>
<option value="multi">*</option>
<option value="invalid">**</option>
<option value="divide">/</option>
</select>
<input type='text' name='input2' size='15'>
<input type='submit' value='go'>
</form>
This is php with if elseif statement
<?php
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$result = null;
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
elseif
(!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$result = INVALID_INPUT;
}
if ($result !== null)
{
echo <<<EOM
<h2>you calculate</h2> $input1
<h2>and</h2> $input2
<h2>result is:</h2>
$result
EOM;
}
?>
I even tried with only else statement and without condition but it wont show INVALID INPUT when nothing is added in form. What is wrong here?
The problem is with this piece of code:
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
Even if a user enters nothing in the input fields, they will still pass isset($_POST['input1']), since they are 'set' to an empty string. Try switching it to this instead:
if
(isset($_POST['input1']) && strlen($_POST['input1']) &&
isset($_POST['input2']) && strlen($_POST['input2']) &&
isset($_POST['operation']))
You could just use an else statement instead of an else if statement. That way if you change the inputs you don't have to list each of them twice.
eg:
if (isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
// result = calculation
} else
{
// result == code
}
Try this - I tested this on my server and it works!
calculator.php
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',1);;
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$input1 = $input2 = $operation = null;
if (isset($_POST['input1']) and isset($_POST['input2']) and isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
if(is_numeric($input1) and is_numeric($input2))
{
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
else
{
$input1 = '';
$input2 = '';
$operation = 'invalid';
$result = INVALID_INPUT;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="">
<form method='POST' action='calculator.php'>
<input type='text' name='input1' size='15' value="<?php echo $input1; ?>">
<select name='operation'>
<option <?php if($operation == 'plus') { echo 'selected';} ?> value="plus">+</option>
<option <?php if($operation == 'minus') { echo 'selected';} ?> value="minus">-</option>
<option <?php if($operation == 'multi') { echo 'selected';} ?> value="multi">*</option>
<option <?php if($operation == 'invalid') { echo 'selected';} ?> value="invalid">**</option>
<option <?php if($operation == 'divide') { echo 'selected';} ?> value="divide">/</option>
</select>
<input type='text' name='input2' size='15' value="<?php echo $input2; ?>">
<span>=</span>
<input type='text' name='result' size='50' value="<?php echo $result; ?>">
<input type='submit' value='go'>
</body>
</html>
Your condition is wrong; this is impossible:
!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation'])
I suspect you mean
!isset($_POST) or
!isset($_POST['input1']) or
!isset($_POST['input2']) or
!isset($_POST['operation'])
I'm creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it's not outputting the answer to the browser.
Here are the codes for my html and PHP files:
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="post" attribute="post" action="disp_form.php">
<p>First Value:<br/>
<input type="text" id="first" name="first"></p>
<p>Second Value:<br/>
<input type="text" id="second" name="second"></p>
<input type="radio" name="group1" id="add" value="add" checked="true"><p>+</p><br/>
<input type="radio" name="group1" id="subtract" value="subtract"><p>-</p><br/>
<input type="radio" name="group1" id="times" value="times"><p>x</p><br/>
<input type="radio" name="group1" id="divide" value="divide"><p>/</p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
PHP file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
<?php
$result = "";
class calculator
{
var $a;
var $b;
function checkopration($oprator)
{
switch($oprator)
{
case '+':
return $this->a + $this->b;
break;
case '-':
return $this->a - $this->b;
break;
case '*':
return $this->a * $this->b;
break;
case '/':
return $this->a / $this->b;
break;
default:
return "Sorry No command found";
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkopration($c);
}
}
$cal = new calculator();
if(isset($_POST['submit']))
{
$result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}
?>
<form method="post">
<table align="center">
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" name="n2"></td>
</tr>
<tr>
<td>Select Oprator</td>
<td><select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" = "></td>
</tr>
</table>
</form>
Personally I would do a switch instead of all this if, else if, else
$first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
$second= $_POST['second'] + 0;
$operator = $_POST["group1"];
switch($operator)
{
case "add"
echo "Answer is: " .$first + $second;
break;
case "subtract"
echo "Answer is: " .$first - $second;
break;
case "times"
echo "Answer is: " .$first * $second;
break;
case "divide"
echo "Answer is: " .$first / $second;
break;
}
You need to assign $first and $second
$first = $_POST['first'];
$second= $_POST['second'];
Also, As Travesty3 said, you need to do your arithmetic outside of the quotes:
echo $first + $second;
You also need to put the [== 'add'] math operation into quotes
if($_POST['group1'] == 'add') {
echo $first + $second;
}
complete code schould look like that :
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
?>
You need to get the values the same way to get the calculator operation which looks like:
<?php
if($_POST['group1'] == add) {
echo "$_POST['first']+ $_POST['second'];
}
... and so on
?>
Or, to make it easier, just do:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
$first = doubleval($_POST['first']);
$second = doubleval($_POST['second']);
if($_POST['group1'] == 'add') {
echo "$first + $second = ".($first + $second);
}
// etc
Check string using single quotes
Ex. $_POST['group1'] == 'add'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
HTML Code is here:
<form method="post">
<input type="text" name="numb1">
<input type="text" name="numb2">
<select name="operator" id="">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
<option>Square</option>
</select>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['numb1'];
$result2 = $_POST['numb2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'None':
echo "You need to select any operator";
break;
case 'Add':
echo $result1 + $result2;
break;
case 'Subtract':
echo $result1 - $result2;
break;
case 'Multiply':
echo $result1 * $result2;
break;
case 'Divide':
echo $result1 / $result2;
break;
case 'Square':
echo $result1 ** $result2;
break;
}
}
?>
enter code here
</body>
</html>
Make this changes in php file
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
else {
echo "Enter the numbers properly";
}
?>
</p>
</body>
</html>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="GET">
<h1>Calculator</h1>
<p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
<ul>
<ol> Multiplication: * </ol>
<ol> Substraction: - </ol>
<ol> Division: / </ol>
<ol> Addition: + </ol>
</ul>
<p>Enter first number:</p>
<input type="text" name="cal1">
<p>Enter second number:</p>
<input type="text" name="cal2">
<p>Enter the calculator symbol</p>
<input type="text" name="symbol">
<button>Calculate</button>
</form>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
</body>
</html>
So, i made a seat booking system where there are checkboxes in every seat and the user select this boxes and the seats are considered reserved.
But now i want make the user be able to give each marked seat a name. How can i do that?
seats.php
<?php include("login.php"); ?>
<html>
<head>
<title>Tickets</title>
<style>
* {
font-size: 11px;
font-family: arial;
}
</style>
<script>
function reserveSeats() {
var selectedList = getSelectedList('Reserve Seats');
if (selectedList) {
if (confirm('Do you want to reserve
selected seat/s '
+ selectedList + '?')) {
document.forms[0].oldStatusCode.value=0;
document.forms[0].newStatusCode.value=1;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}
function cancelSeats() {
var selectedList = getSelectedList('Cancel Reservation');
if (selectedList) {
if (confirm('Do you want to cancel reserved seat/s ' + selectedList + '?')) {
document.forms[0].oldStatusCode.value=1;
document.forms[0].newStatusCode.value=0;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}
function confirmSeats() {
var selectedList = getSelectedList('Confirm Reservation');
if (selectedList) {
if (confirm('Do you want to confirm reserved seat/s ' + selectedList + '?')) {
document.forms[0].oldStatusCode.value=1;
document.forms[0].newStatusCode.value=2;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}
function getSelectedList(actionSelected) {
// get selected list
var obj = document.forms[0].elements;
var selectedList = '';
for (var i = 0; i < obj.length; i++) {
if (obj[i].checked && obj[i].name == 'seats[]') {
selectedList += obj[i].value + ', ';
}
}
// no selection error
if (selectedList == '') {
alert('Please select a seat before clicking ' + actionSelected);
return false;
} else {
return selectedList;
}
}
function clearSelection() {
var obj = document.forms[0].elements;
for (var i = 0; i < obj.length; i++) {
if (obj[i].checked) {
obj[i].checked = false;
}
}
}
function refreshView() {
clearSelection();
document.forms[0].action='<?php echo $_SERVER['PHP_SELF']; ?>';
document.forms[0].submit();
}
</script>
</head>
<body>
<table>
<tr><td width="100%" align="center">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="oldStatusCode" value=""/>
<input type="hidden" name="newStatusCode" value=""/>
<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Refresh View' onclick='refreshView();'/>
</td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Reserve Seats' onclick='reserveSeats()'/>
<input type='button' value='Confirm Reservation' onclick='confirmSeats()'/>
<input type='button' value='Cancel Reservation' onclick='cancelSeats()'/>
</td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Clear Selection' onclick='clearSelection()'/></td>
</tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<?php
/*
* Created on Mar 17, 2007
* Author: dayg
*/
$linkID = # mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
# mysql_select_db("tickets") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT * from seats order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='3' cellspacing='3'>";
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
if ($rowId != 'A') {
echo "</tr></table></td>";
echo "\n</tr>";
}
$prevRowId = $rowId;
echo "\n<tr><td align='center'><table border='1' cellpadding='8' cellspacing='8'><tr>";
} else {
$tableRow = false;
}
if ($status == 0) {
$seatColor = "lightgreen";
} else if ($status == 1 && $updatedby == 'user1') {
$seatColor = "FFCC99";
} else if ($status == 1 && $updatedby == 'user2') {
$seatColor = "FFCCFF";
} else if ($status == 2 && $updatedby == 'user1') {
$seatColor = "FF9999";
} else if ($status == 2 && $updatedby == 'user2') {
$seatColor = "CC66FF";
} else {
$seatColor = "red";
}
echo "\n<td bgcolor='$seatColor' align='center'>";
echo "$rowId$columnId";
if ($status == 0 || ($status == 1 && $updatedby == $_SERVER['PHP_AUTH_USER'])) {
echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
}
echo "</td>";
if (($rowId == 'A' && $columnId == 7)
|| ($rowId == 'B' && $columnId == 9)
|| ($rowId == 'C' && $columnId == 9)
|| ($rowId == 'D' && $columnId == 10)
|| ($rowId == 'E' && $columnId == 8)
|| ($rowId == 'F' && $columnId == 5)
|| ($rowId == 'G' && $columnId == 13)
|| ($rowId == 'H' && $columnId == 14)
|| ($rowId == 'I' && $columnId == 14)
|| ($rowId == 'J' && $columnId == 12)
|| ($rowId == 'K' && $columnId == 14)
|| ($rowId == 'L' && $columnId == 13)
|| ($rowId == 'M' && $columnId == 9)) {
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td> </td>";
}
}
echo "</tr></table></td>";
echo "</tr>";
echo "</table>";
/* Close connection to database server. */
mysql_close();
?>
</td></tr>
<tr><td> </td></tr>
<tr><td width="100%" align="center">
<table border="1" cellspacing="8" cellpadding="8">
<tr>
<td bgcolor='lightgreen'>Available</td>
<td bgcolor='FFCC99'>Reserved user1</td>
<td bgcolor='FF9999'>Confirmed user1</td>
<td bgcolor='FFCCFF'>Reserved user2</td>
<td bgcolor='CC66FF'>Confirmed user2</td>
</tr>
</table>
</td></tr>
<tr><td> </td></tr>
<tr><td width="100%" align="center">
View Layout
</td></tr>
</table>
</form>
</body>
</html>
bookseats.php
<html>
<head>
<title>Book Seats</title>
<style>
* {
font-size: 14px;
font-family: arial;
}
</style>
</head>
<body>
<?php include("login.php"); ?>
<center>
<br/>
<br/>
<br/>
<?php
if (isset($_POST['seats']))
{
$user = $_SERVER['PHP_AUTH_USER'];
$newStatusCode = $_POST['newStatusCode'];
$oldStatusCode = $_POST['oldStatusCode'];
// open database connection
$linkID = # mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
# mysql_select_db("tickets") or die("Could not select database");
// prepare select statement
$selectQuery = "SELECT rowId, columnId from seats where (";
$count = 0;
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$selectQuery .= " || ";
}
$selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
$selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";
$count++;
}
$selectQuery .= " ) and status = $oldStatusCode";
if ($oldStatusCode == 1) {
$selectQuery .= " and updatedby = '$user'";
}
//echo $selectQuery;
// execute select statement
$result = mysql_query($selectQuery);
//echo $result;
$selectedSeats = mysql_num_rows($result);
//echo "<br/>" . $selectedSeats;
if ($selectedSeats != $count) {
$problem = "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
$problem .= "Possible problems are:";
$problem .= "<ul>";
$problem .= "<li>Another process was able to book the same seat while you were still browsing.</li>";
$problem .= "<li>You were trying to Confirm an unreserved Seat.</li>";
$problem .= "<li>You were trying to Cancel an unreserved Seat.</li>";
$problem .= "<li>You were trying to Reserve a reserved Seat.</li>";
$problem .= "<li>There was a problem connecting to the database.</li>";
$problem .= "</ul>";
$problem .= "<a href='seats.php'>View Seat Plan</a>";
die ($problem);
}
// prepare update statement
$newStatusCode = $_POST['newStatusCode'];
$oldStatusCode = $_POST['oldStatusCode'];
$updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";
$count = 0;
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$updateQuery .= " || ";
}
$updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
$updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";
$count++;
}
$updateQuery .= " ) and status = $oldStatusCode";
if ($oldStatusCode == 1) {
$updateQuery .= " and updatedby = '$user'";
}
// perform update
$result = mysql_query($updateQuery);
$updatedSeats = mysql_affected_rows();
if ($result && $updatedSeats == $count) {
//$mysql->commit();
echo "<h3>";
echo "You have successfully updated $updatedSeats seat/s: ";
echo "[";
foreach($_POST['seats'] AS $seat) {
$rowId = substr($seat, 0, 1);
$columnId = substr($seat, 1);
echo $rowId . $columnId . ", ";
}
echo "]";
echo "...</h3>";
} else {
//$mysql->rollback();
echo "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
echo "Possible problems are:";
echo "<ul>";
echo "<li>Another process was able to book the same seat while you were still browsing.</li>";
echo "<li>You were trying to Confirm an unreserved Seat.</li>";
echo "<li>You were trying to Cancel an unreserved Seat.</li>";
echo "<li>You were trying to Reserve a reserved Seat.</li>";
echo "<li>There was a problem connecting to the database.</li>";
echo "</ul>";
}
echo "<a href='seats.php'>View Seat Plan</a>";
// Enable the autocommit feature
//$mysqldb->autocommit(TRUE);
// Recuperate the query resources
//$result->free();
mysql_close();
}
?>
</center>
</body>
</html>
EDIT:
I tried the following:
When the user submit the checkboxes, this is included:
name.php
<?php
//$mysql->commit();
session_start();
$_SESSION['rowId'] = $_POST['rowId'];
$_SESSION['columnId'] = $_POST['columnId'];
echo "<h3>";
echo "Please enter the name for each seat:<br><p> </p>";
echo "";
foreach($_POST['seats'] AS $seat) {
$rowId = substr($seat, 0, 1);
$columnId = substr($seat, 1);
echo $rowId . $columnId . '</br><form method="post" name="input" action="pt2.php" >
<input name="name" type="text"/></br>';
}
?>
<input type="submit" name="Submit" value="insert" />
</form>
pt2.php:
<?php
// Connect to MySQL
mysql_connect("localhost", "root", "root") or die("Connection Failed");
mysql_select_db("tickets")or die("Connection Failed");
$name = $_POST['name'];
session_start();
$rowId = $_SESSION['rowId'];
$columnId = $_SESSION['columnId'];
$result = mysql_query("UPDATE seats SET updatedby='".$name."' WHERE rowId='".$rowId."' AND columnId='".$columnId."'")
or die(mysql_error());
?>
If the user selects only one seat, this code works like a charm. But when there's two or more, the $columnId and the $rowId doesn't change as it should, and only the name of the last checked seat is changed.
i think you shouldn't use jquery for this part. try using php with an html form ( show an input next to the seat it has the checkbox ) then save the data into mysql seatid, seat_userId, seat_name etc... then just pull the data and do whatever you want with it.
Please how can I get the status of radio buttons based on their value in the database?
I want to set them to either checked or unchecked based on their respective values in the database. field_type is int(1 or 0).
Is there a more efficient way to do this?
$news_alert_status_a = "unchecked";
$news_alert_status_k = "unchecked";
$news_alert_status_n = "unchecked";
$events_alert_status_a = "unchecked";
$events_alert_status_k = "unchecked";
$events_alert_status_n = "unchecked";
$questions_alert_status_a = "unchecked";
$questions_alert_status_k = "unchecked";
$questions_alert_status_n = "unchecked";
$editorials_alert_status_a = "unchecked";
$editorials_alert_status_k = "unchecked";
$editorials_alert_status_n = "unchecked";
if ($news_alert == 1)
{
$news_alert_status_a = "checked";
}
elseif ($news_alert == 2)
{
$news_alert_status_k = "checked";
}
elseif($news_alert == 3)
{
$news_alert_status_n = "checked";
}
if ($events_alert == 1)
{
$events_alert_status_a = "checked";
}
elseif ($events_alert == 2)
{
$events_alert_status_k = "checked";
}
elseif($events_alert == 3)
{
$events_alert_status_n = "checked";
}
if ($questions_alert == 1)
{
$questions_alert_status_a = "checked";
}
elseif ($questions_alert == 2)
{
$questions_alert_status_k = "checked";
}
elseif($questions_alert == 3)
{
$questions_alert_status_n = "checked";
}
if ($editorials_alert == 1)
{
$editorials_alert_status_a = "checked";
}
elseif ($editorials_alert == 2)
{
$editorials_alert_status_k = "checked";
}
elseif($editorials_alert == 3)
{
$editorials_alert_status_n = "checked";
}
The PHP code:
<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['gender'];
if ($selected_radio = = 'male') {
$male_status = 'checked';
}
else if ($selected_radio = = 'female') {
$female_status = 'checked';
}
}
?>
The HTML FORM code:
<FORM name ="form1" method ="post" action ="radioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'
<?PHP print $male_status; ?>
>Male
<Input type = 'Radio' Name ='gender' value= 'female'
<?PHP print $female_status; ?>
>Female
<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>
<?php
if($_POST)
{
if($_POST['gender'] != "")
{
$gender = $_POST['gender'] == "male" ? 1 : 0;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Radio Button</title>
</head>
<body>
<form method="post">
<label for="radio_female">Female</label><input type="radio" name="gender" id="radio_female" value="female"/>
<label for="radio_male">Male</label><input type="radio" name="gender" id="radio_male" value="male"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>