How to store prompt() value in to the Database in php - javascript

we have a program that has two button(approve and disapprove). Our problem is when i hit on the button, the value on the prompt doesn't save on the database.
ServiceRequest.php
<?php
session_start();
if(!isset($_SESSION['user']) || !isset($_SESSION['lname']) || !isset($_SESSION['fname']) || !isset($_SESSION['lname']) || !isset($_SESSION['pass']) || !isset($_SESSION['assign']) || !isset($_SESSION['department']) || !isset($_SESSION['branch']) || !isset($_SESSION['province']) || !isset($_SESSION['position']))
{
header('Location:login.php');
}
include 'config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="image/fblogo.ico">
<title>FB | ITS&CMS</title>
<link href="dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- <link href="signin.css" rel="stylesheet"> -->
<link rel="stylesheet" type="text/css" href="dist/css/jquery.dataTables.min.css"></script>">
<link rel="stylesheet" type="text/css" href="css/body.css">
<script src="assets/js/ie-emulation-modes-warning.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="assets/js/ie10-viewport-bug-workaround.js"></script>
<script src="dist/js/jquery-1.12.4.js"></script>
<script src="dist/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#otable').DataTable({
"searching": false,
"order" : [[ 5, "desc" ]]
});
$("#search").click(function(){
$("#searchPanelWrap").slideToggle("slow");
});
$("#searchServiceRequests").click(function(){
var filterVal ="";
filterVal = "srfno=" + $('#srf').val() + "&branch=" + $('#branch').val() + "&dept=" + $('#dept').val() + "&reqf=" + $('#datereqfrom').val() + "&reqt=" + $('#datereqto').val() + "";
document.location.href = 'servicereq.php?s=1&' + filterVal;
});
});
</script>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<?php
include "header.php";
?>
</nav>
<br><br><br><br><br>
<div class="form">
<center>
<fieldset id="fieldsetform">
<div class="form"><br><br>
<center><h4>Service Request Forms</h4></center>
<ul class="tab-group"><br>
<button style="width: 150px; height: 50px;" type="button" class="btn btn-success" value="Service">Service Request</button>
<button style="width: 150px; height: 50px;" type="button" class="btn btn-danger" value="Service">Close Request</button>
<button class="btn" style="width: 50px; height: 50px;" value="Search" id="search"><span class="glyphicon glyphicon-filter"></span></btn>
</ul>
<div id="searchPanelWrap" style="padding-bottom:10px; display:none;">
<input style = "text" placeholder = "SRF No." id="srf"/>
<input style = "text" placeholder = "Branch" id="branch"/>
<input style = "text" placeholder = "Department" id="dept"/>
Request Date From: <input style = "text" type = "date" value="" id="datereqfrom"/>
Request Date To: <input style = "text" type = "date" value="" id="datereqto"/>
<button class="btn btn-default" id="searchServiceRequests">Search</button>
</div>
<form action="downloadfile.php" method="get"></form>
<?php
//if(!$uname == 'uname' && !$pass = 'pass'){
include 'dbconn.php';
if($_SESSION['assign']!="Main Approver"){
$clause = "dept = '".$_SESSION['department']."' AND city='".$_SESSION['branch']."' AND type != '".$_SESSION['assign']."' AND status = 'PENDING' ";
}else{
$clause = "status = 'PENDING' ";
}
if($_GET["s"] == "1"){
$srf = $_GET["srfno"];
$branch = $_GET["branch"];
$dept = $_GET["dept"];
$datereqfrom = $_GET["reqf"];
$datereqto = $_GET["reqt"];
$srfFilter = "";
$brFilter = "";
$deptFilter = "";
$reqFilter = "";
if($srf != ""){
$srfFilter = "AND SRFNo = '".$srf."' ";
}
if($branch != ""){
$brFilter = "AND city = '".$branch."' ";
}
if($dept != ""){
$deptFilter = "AND dept = '".$dept."' ";
}
if($datereqfrom != "" && $datereqto != ""){
$reqFilter = "AND RequestDateTime BETWEEN '".$datereqfrom." 00:00:00.000' AND '".$datereqto." 23:59:59.999' ";
}
$addFilter = $srfFilter.$brFilter.$deptFilter.$reqFilter;
}else{
$addFilter = "";
}
$record= getAllServicePending($clause.$addFilter);
echo '<table id="otable" class="table table-striped">';
echo '<thead>';
echo '<th>SRF No.</th>';
echo '<th>Last Name</th>';
echo '<th>First Name</th>';
echo '<th>Branch</th>';
echo '<th>Department</th>';
echo '<th>Date Requested</th>';
echo '<th>Attached File</th>';
echo '<th>Service Form View</th>';
echo '<th>Status</th>';
echo '<th>Action</th>';
echo '</thead>';
echo "<tbody>";
foreach($record as $r){
$i=0;
echo '<tr>';
$idno;
foreach($r as $fields){
if($i==0) $idno=$fields;
//echo '<td>'.strtoupper($fields).'</td>';
$i++;
}
$files = $r['file'];
echo '<td>'.$r['SRFNo'].'</td>';
echo '<td>'.$r['lastreq'].'</td>';
echo '<td>'.$r['firstreq'].'</td>';
echo '<td>'.$r['city'].'</td>';
echo '<td>'.$r['dept'].'</td>';
echo '<td>'.$r['RequestDateTime'].'</td>';
echo '<td>'.$r['file'].'</td>';
echo '<td>Service Request</td>';
echo '<td>'.$r['status'].'</td>';
echo '<td>
<button type="button" class="btn btn-success" onclick="window.location.href=`approveserv.php?idno='.$idno.'&status=approve`" style="width:136px;"><span class="glyphicon glyphicon-thumbs-up" style="padding-right:5px"></span>APPROVE</button><br>
<button type="button" class="btn btn-danger" onclick="window.location.href=`approveserv.php?idno='.$idno.'&status=disapprove`" style="width:136px;"><span class="glyphicon glyphicon-thumbs-down" style="padding-right:5px"></span>DISAPPROVE</button>
</td>';
echo '</tr>';
}
echo "</tbody>";
echo '</table>';
//}
?><br><br>
</div>
</fieldset>
</center>
</div><?php
include "footer.php";
?>
</body>
</html>
Here is how I used the prompt(), this will just perform the SQL for approve and disapporve but the value of the prompt does not store to the database.
SQL query:
approveserv.php<?php
session_start();
if(!isset($_SESSION['user']) || !isset($_SESSION['lname']) || !isset($_SESSION['fname']) || !isset($_SESSION['lname']) || !isset($_SESSION['pass']) || !isset($_SESSION['assign']) || !isset($_SESSION['department']) || !isset($_SESSION['branch']) || !isset($_SESSION['province']) || !isset($_SESSION['position']))
{
header('Location:login.php');
}
include ('dbconn.php');
$idno = $_GET["idno"];
$reqstat = $_GET["status"];
$s = ret_status_service($idno);
$user = $_SESSION['user'];
$db = site_db();
if($reqstat=="approve"){
$sql = "UPDATE services SET status ='APPROVED', ApprovalDateTime = CURRENT_TIMESTAMP(), ApprovedBy = '".$user."' WHERE ser_id = ?";
}else{
$sql = "UPDATE services SET status ='DISAPPROVED', ApprovalDateTime = CURRENT_TIMESTAMP(), ApprovedBy = '".$user."' WHERE ser_id = ?";
}
$s = $db->prepare($sql);
$s->execute(array($idno));
$db = null;
echo "<script>if(prompt('Successfully ${reqstat}d request.')){document.location.href='servicereq.php?s=0'};</script>";
// header('location:servicereq.php');
?>

If your concerns are how to get the input of prompt dialog and send it to servicereq.php. You could refer to the following code:
//servicereq.php - to get the prompt message from approveserv.php and store it to database
if(isset($_REQUEST['prompt_msg'])){
$prompt_msg= $_REQUEST['prompt_msg'];
// you could continue to store it in the database
}
//approveserv.php - to output the javascript and get the prompt message and send it to servicereq.php
echo "<script>if(prompt_msg=prompt('Successfully ".$reqstat."d action request.')){document.location.href='servicereq.php?prompt_msg='+prompt_msg+'&s=0'};</script>";
Note: I suppose you could manage to store the prompt msg to the database, so I didn't write the code here.
Here is a further material for how to use prompt.

Related

How to update quantity of database from php action

I am building a database of different devices which is displayed in a page which sells medical devices. When a user adds a quantity of the device to the cart, I want the database to be updated with (stock - quantity in cart) I am trying to do this on PHP but am having no luck. My attempt and code is below.
Here is a snippet of my attempt. I'm not sure where to place this in the code below.
<?php
$value = isset($_POST['item']) ? $_POST['item'] : 1; //to be displayed
if(isset($_POST['incqty'])){
$value += 1;
$query = "UPDATE products SET stock= (stock-$product_qty) WHERE product_name=$product_name";
mysql_select_db('products');
$retval = mysql_query($query,$mysqli);
}
?>
This is code for index.php
<?php
session_start();
include_once("config.php");
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 align="center">Products </h1>
<!-- View Cart Box Start -->
<?php
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{
echo '<div class="cart-view-table-front" id="view-cart">';
echo '<h3>Your Shopping Cart</h3>';
echo '<form method="post" action="cart_update.php">';
echo '<table width="100%" cellpadding="6" cellspacing="0">';
echo '<tbody>';
$total =0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_color = $cart_itm["product_color"];
$bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe
echo '<tr class="'.$bg_color.'">';
echo '<td>Qty <input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
echo '<td>'.$product_name.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /> Remove</td>';
echo '</tr>';
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
echo '<td colspan="4">';
echo '<button type="submit">Update</button>Checkout';
echo '</td>';
echo '</tbody>';
echo '</table>';
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
?>
<!-- View Cart Box End -->
<!-- Products List Start -->
<?php
$results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img_name, price, stock FROM products ORDER BY id ASC");
if($results){
$products_item = '<ul class="products">';
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
$products_item .= <<<EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content"><h3>{$obj->product_name}</h3>
<div class="product-thumb"><img src="images/{$obj->product_img_name}"></div>
<div class="product-desc">{$obj->product_desc}</div>
<div class="product-info">
Price {$currency}{$obj->price}
<fieldset>
<label>
<span>Color</span>
<select name="product_color">
<option value="Black">Black</option>
<option value="Silver">Silver</option>
</select>
</label>
<label>
<span>Quantity</span>
<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</label>
</fieldset>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<div align="center"><button type="submit" id="updateb" class="add_to_cart">Add</button></div>
</div></div>
</form>
</li>
EOT;
}
$products_item .= '</ul>';
echo $products_item;
}
?>
<!-- Products List End -->
</body>
</html>
This is code for cart_update.php
<?php
session_start();
include_once("config.php");
//add product to session or create new one
if(isset($_POST["type"]) && $_POST["type"]=='add' && $_POST["product_qty"]>0)
{
foreach($_POST as $key => $value){ //add all post vars to new_product array
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
//remove unecessary vars
unset($new_product['type']);
unset($new_product['return_url']);
//we need to get product name and price from database.
$statement = $mysqli->prepare("SELECT product_name, price, stock FROM products WHERE product_code=? LIMIT 1");
$statement->bind_param('s', $new_product['product_code']);
$statement->execute();
$statement->bind_result($product_name, $price, $stock);
while($statement->fetch()){
//fetch product name, price from db and add to new_product array
$new_product["product_name"] = $product_name;
$new_product["product_price"] = $price;
$new_product["product_stock"] = $stock;
if(isset($_SESSION["cart_products"])){ //if session var already exist
if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check item exist in products array
{
unset($_SESSION["cart_products"][$new_product['product_code']]); //unset old array item
}
}
$_SESSION["cart_products"][$new_product['product_code']] = $new_product; //update or create product session with new item
}
}
//update or remove items
if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
{
//update item quantity in product session
if(isset($_POST["product_qty"]) && is_array($_POST["product_qty"])){
foreach($_POST["product_qty"] as $key => $value){
if(is_numeric($value)){
$_SESSION["cart_products"][$key]["product_qty"] = $value; //change
}
}
}
//remove an item from product session
if(isset($_POST["remove_code"]) && is_array($_POST["remove_code"])){
foreach($_POST["remove_code"] as $key){
unset($_SESSION["cart_products"][$key]);
}
}
}
//back to return url
$return_url = (isset($_POST["return_url"]))?urldecode($_POST["return_url"]):''; //return url
header('Location:'.$return_url);
?>
This is code for view_cart.php
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View shopping cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css"></head>
<body>
<h1 align="center">View Cart</h1>
<div class="cart-view-table-back">
<form method="post" action="cart_update.php">
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>Total</th><th>Remove</th></tr></thead>
<tbody>
<?php
if(isset($_SESSION["cart_products"])) //check session var
{
$total = 0; //set initial total value
$b = 0; //var for zebra stripe table
foreach ($_SESSION["cart_products"] as $cart_itm)
{
//set variables to use in content below
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_color = $cart_itm["product_color"];
$subtotal = ($product_price * $product_qty); //calculate Price x Qty
$bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe
echo '<tr class="'.$bg_color.'">';
echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
echo '<td>'.$product_name.'</td>';
echo '<td>'.$currency.$product_price.'</td>';
echo '<td>'.$currency.$subtotal.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></td>';
echo '</tr>';
$total = ($total + $subtotal); //add subtotal to total var
}
$grand_total = $total + $shipping_cost; //grand total including shipping cost
foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount; //add tax val to grand total
}
$list_tax = '';
foreach($tax_item as $key => $value){ //List all taxes
$list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
}
$shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
}
?>
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
<tr><td colspan="5">Add More Items<button type="submit">Update</button></td></tr>
</tbody>
</table>
<input type="hidden" name="return_url" value="<?php
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>
</div>
</body>
</html>
your variables $product_qty and $product_name are not defined and in the query pass the product name in the quotes as your query is in double quotes so pass name in the single quotes.
Also it will be better if you update using primary id not by name.

Uncaught reference: function not defined

I am working on a small project. I have created a PHP page in that as soon as a button in clicked a function named sendMessage should get executed but instead it is showing me uncaught reference error.
My error:
Uncaught ReferenceError:sendMessage is not defined at HTMLInputElement.onclick
line 29. At last onclick call
Here is my js file named chat.js:
function btn() {
var s = document.getElementById("chat_id");
var o = s.options[s.selectedIndex].value;
var x = document.getElementsByClassName("chat");
x[0].style.display = "inline-block";
x[1].innerHTML = "<span>" + "Chatting with:" + o + "<span>";
}
function sendMessage() {
var msg = document.getElementById("msg").value;
if (msg.length === 0 || msg === "") {
alert("please enter some message");
return;
}
var sender = document.getElementById("username").value;
var sendto = document.getElementById("chat_id").options[document.getElementById("chat_id").selectedIndex].value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 400) {
document.getElementById("chat_logs").innerHTML = xhttp.responseText;
}
xhttp.open('GET', 'send_messages.php?sender=' + sender + 'sendto=' + sendto + 'message=' + msg, true);
xhttp.send();
}
}
function test() {
alert("lol");
}
My PHP code:
<?php
include ('db.php');
session_start();
if(empty($_SESSION['user_logs'])){ //check if user session is set
header('Location:login.php'); //redirect to login page if user session is not set
}
?>
<!DOCTYPE html>
<html>
<head>
<title>chat box</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="chat.js"></script>
<link rel="stylesheet" href="chatbox.css" />
</head>
<body>
<div class="container cont">
<p>
Logout <?php echo $_SESSION['user_logs']; ?>
</p>
<input type="hidden" id="username" value=<?php echo $_SESSION[ 'user_logs']; ?>>
<p>
<label for="chatwith">Chat with:</label>
<select name="chat_user" id="chat_id" class="selectpicker">
<option disabled selected>select</option>
<?php
$current_user=$_SESSION['user_logs'];
$rows=mysqli_query($con,"select username from users where username!='".$current_user."'");
while($rowsArray=mysqli_fetch_array($rows)):
?>
<option value=<?php echo $rowsArray[ 'username']; ?>>
<?php echo $rowsArray['username']; ?>
</option>
<?php endwhile;?>
</select>
<input type="button" id="btn_chat" name="toggle" value="chat" class="btn btn-info" onclick="btn();" />
<div id="chat_div" class="chat">
<span class="chat">Chatting with:</span>
<div class="chat" id="chat_logs"></div>
<div class="chat" id="input">
<textarea value="" placeholder="Enter message" id="msg" name="message" required maxlength="150"></textarea>
<input type="button" id="btn-send" class="btn btn-primary" value="send" onclick="sendMessage();" />
</div>
</div>
</div>
</body>
</html>

hide and show div file (logged in)

How I can hide this - "<div class="show1">" when user are not logged in and show only when user are logged in?
This is my code --->>>
<html><head>
<meta charset="utf-8">
<title>100% | Register</title>
<link rel="stylesheet" type="text/css" href="main/home.css"/>
<link rel="stylesheet" type="text/css" href="main/register.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script></head><body><div id="container">
<?php include 'header.php'; ?>
<br/><br/><div class="show1">TEXT</div><div class="show1">TEXT</div>
<div class="hidden1"><div class="right-login" id="container-login"><div id="palabi5px">
<form method="post" class="formpost" id="nonono">
<input type="text" class="username" name="username0" placeholder="Username" autocomplete="off">
<input type="password" class="password" name="password0" placeholder="********">
<button type="submit" name="login" class="loging">Login</button>
</form>
---------------------------------
<br/><br/>
Text!<br/><br/>
<br/>
---------------------------------
<br/><br/></div></div></div>
<?php include 'saites.php'; ?></body></html>
<?php
session_start();
require('connect.php');
$username0 = #$_POST['username0'];
$password0 = #$_POST['password0'];
if(isset($_POST['login'])){
if($username0 && $password0) {
$check = mysqli_query($connect," SELECT * FROM users WHERE username='".$username0."'");
$rows = mysqli_num_rows($check);
if(mysqli_num_rows($check) != 0){
while($row = mysqli_fetch_assoc($check)){
$db_username0 = $row['username'];
$db_password0 = $row['password'];
}
if($username0 == $db_username0 && ($password0) == $db_password0){
#$_SESSION["username"] = $username;
echo "<script type='text/javascript'>
$(document).ready(function(){
alert('page loaded'); // alert to confirm the page is loaded
$('.hidden1').hide(); //enter the class or id of the particular html element which you wish to hide.
$('.show1').show(); //SHOW
});
</script>";
}else{
echo "<script>{ alert('Your pass id wrong.');}</script>";
}
}else{
echo "<script>{ alert('Couldn't find username.');}</script>";
}
}else{
echo "<script>{ alert('Please fill in all the fields.');}</script>";
}
}
?>
<div class="show1">TEXT</div><div class="show1">TEXT</div>
Replace with following code
if(isset($_SESSION["username"])){
echo '<div class="show1">TEXT</div><div class="show1">TEXT</div>';
}

JavaScript not working properly in Safari

So i made this countdown generator; it's based on a PHP input to fill the JavaScript, the problem is that in Safari it doesn't show this:
or
Is there a way to fix it in Safari? I've been thinking of disabling that input when in Safari and enabling a more basic numbers one but I don't know how to detect, is there another fix?
My code:
Form page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://monopolo11.ninja/tests/css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://monopolo11.ninja/tests/js/dropdown.js"></script>
<title>Countdown Maker</title>
</head>
<body class="lato">
<?php include("assets/navbartools.html");?>
<div class="lato container-fluid text-center" id="contents">
<p>Introduce la fecha</p>
<form action="countdownres.php" method="get" >
Fecha:
<input type="date" name="datein">
<br/>
Hora:
<input type="time" action="countdownres.php" method="get" name="timein">
<p>Cuantos argumentos quieres que se visualicen:</p>
<input type="checkbox" name="mil" value="1">Milenios
<input type="checkbox" name="cen" value="1">Siglos
<input type="checkbox" name="dec" value="1">Decadas
<input type="checkbox" name="yr" checked value="1">Años
<input type="checkbox" name="mo" checked value="1">Meses
<input type="checkbox" name="wk" value="1">Semanas
</br>
<input type="checkbox" name="da" checked value="1">Dias
<input type="checkbox" name="h" checked value="1">Horas
<input type="checkbox" name="m" checked value="1">Minutos
<input type="checkbox" name="s" checked value="1">Segundos
<input type="checkbox" name="mili" value="1">Milisegundos
<input type="submit" class="center-block btn btn-primary">
</form>
</div>
</body>
</html>
Result page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://monopolo11.ninja/tests/css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://monopolo11.ninja/tests/js/countdown.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://monopolo11.ninja/tests/js/dropdown.js"></script>
<title>Countdown</title>
</head>
<body class="lato">
<?php include("assets/navbartools.html");?>
<?php
//variables
$date = $_GET[datein];
$date = explode('-', $date);
$time = $_GET[timein];
$time = explode(':', $time);
$hour = $time[0];
$minute = $time[1];
$year = $date[0];
$month = $date[1];
$day = $date[2];
$mil = 0;
$cen = 0;
$dec = 0;
$yr = 0;
$mo = 0;
$wk = 0;
$da = 0;
$h = 0;
$m = 0;
$s = 0;
$mili = 0;
//variables de get
$mil = $_GET[mil];
$cen = $_GET[cen];
$dec = $_GET[dec];
$yr = $_GET[yr];
$mo = $_GET[mo];
$wk = $_GET[wk];
$da = $_GET[da];
$h = $_GET[h];
$m = $_GET[m];
$s = $_GET[s];
$mili = $_GET[mili];
if ($mil == 1){
$mil = "countdown.MILLENNIA";
}
else{$mil=0;};
if ($cen == 1){
$cen = "countdown.CENTURIES";
}
else{$cen=0;};
if ($dec == 1){
$dec = "countdown.DECADES";
}
else{$dec=0;};
if ($yr == 1){
$yr = "countdown.YEARS";
}
else{$yr=0;};
if ($mo == 1){
$mo = "countdown.MONTHS";
}
else{$mo=0;};
if ($wk == 1){
$wk = "countdown.WEEKS";
}
else{$wk=0;};
if ($da == 1){
$da = "countdown.DAYS";
}
else{$da=0;};
if ($h == 1){
$h = "countdown.HOURS";
}
else{$h=0;};
if ($m == 1){
$m = "countdown.MINUTES";
}
else{$m=0;};
if ($s == 1){
$s = "countdown.SECONDS";
}
else{$s=0;};
if ($mili == 1){
$mili = "countdown.MILLISECONDS";
}else{$mili=0;};
?><h1 class="lato text-center">Cuenta Regresiva para el <?php echo $_GET[datein]." ".$_GET[timein];?>
<div id="cuenta" class="lato text-center text-capitalize"><strong></strong></div>
</h1>
<script>
//labels
countdown.setLabels(
' milisegundo| segundo| minuto| hora| dia| semana| mes| año| década| siglo| milenio',
' milisegundos| segundos| minutos| horas| dias| semanas| meses| años| décadas| siglos| milenios',
' y ',
' + ',
'Ahora');
//variables de fechas
var dia = <?php echo $day?>;
var mes = <?php echo $month?> - 1;
var year = <?php echo $year?>;
var hour = <?php echo $hour?>;
var minute = <?php echo $minute?>;
//variables de atributos que se mostraran
var mil = <?php echo $mil?>;
var cen = <?php echo $cen?>;
var dec = <?php echo $dec?>;
var yr = <?php echo $yr?>;
var mo = <?php echo $mo?>;
var wk = <?php echo $wk?>;
var da = <?php echo $da?>;
var h = <?php echo $h?>;
var m = <?php echo $m?>;
var s = <?php echo $s?>;
var mili = <?php echo $mili?>;
var ref =1000;
if (mili == 1){
ref = 1;
}
//countdown clock
var clock1 = document.getElementById("cuenta")
, tdy = new Date();
clock1.innerHTML = countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate()) ).toString();
setInterval(function(){
clock1.innerHTML = countdown(new Date(year, mes, dia, hour, minute),null,mil | cen | dec | yr | mo| wk | da | h | m | s | mili ).toString();
},ref);
console.log(ref);
console.log(mili);
console.log(tdy);
</script>
</body>
</html>

PHP page with Javascript

this is my main.php
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('Location: index.php');
}
include("connection/config.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TOM ELOY CONVENIENCE STORE</title>
<link rel="stylesheet" href="css/design.css" type="text/css" />
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
<link href="css/iconic.css" media="screen" rel="stylesheet" type="text/css" />
<script></script>
</head>
<body>
<div id="wrapper">
<!--header link -->
<div id="sitename" class="clear">
<?php include_once("header.php"); ?>
</div>
<!--menu navigation -->
<div class="wrap">
<nav>
<?php include_once("menunav.php"); ?>
<div class="clearfix"></div>
</nav>
</div>
<div id="body-wrapper">
<!-- body -->
<div id="body" class="clear">
<div class="clear">
<div id="content"></div>
<!-- script for pages -->
<script type="text/javascript" src="js/general.js"></script>
<script src="js/main.js"></script>
</div>
</div>
</div>
<!--footer link -->
<div id="footer" align="center">
<?php include_once("footer.php"); ?>
</div>
</div>
</body>
</html>
this is my javascript the main.js
$(document).ready(function () {
$('#content').load('main2.php');
$('ul#nav li a').click(function() {
var page = $(this).attr('href');
$('#content').load( page + '.php');
return false;
});
});
this is my supplierprofile.php
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('Location: index.php');
}
include("connection/config.php");
?><html>
<head>
<link rel="stylesheet" href="css/styles.css" type="text/css" />
<!-- autorefresh of the table -->
<script type="text/javascript">
function Ajax()
{
var
$http,
$self = arguments.callee;
if (window.XMLHttpRequest) {
$http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
$http = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
$http = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ($http) {
$http.onreadystatechange = function()
{
if (/4|^complete$/.test($http.readyState)) {
document.getElementById('ReloadThis').innerHTML = $http.responseText;
setTimeout(function(){$self();}, 0);
}
};
$http.open('GET', 'supplierprofiletable.php' + '?' + new Date().getTime(), true);
$http.send(null);
}
}
</script>
</head>
<!-- content2 -->
<div id="content">
<p id="bcp">Browse Supplier Profile</p><br><br>
<ul id="nav">
<li>
<a id="abutton">PRINT</a>
<a id="abutton" href="addspform">ADD SUPPLIER</a>
</li>
</ul>
<!-- autorefresh of the table -->
<script type="text/javascript">
setTimeout(function() {Ajax();}, 0);
</script>
<!-- table to be refresh -->
<div id="ReloadThis"><?php include_once("supplierprofiletable.php"); ?></div>
</div>
<? -- script for pages --?>
<script type="text/javascript" src="js/general.js"></script>
<script src="js/submain.js"></script>
this is the supplieprofiletable.php
<table id="spvt" align="center">
<tr>
<th id="spvth">SUPPLIER NAME</th>
<th id="spvth">TERMS DAY</th>
<th id="spvth">VAT</th>
<th id="spvth">MODIFY</th>
<th id="spvth">DELETE</th>
</tr>
<?php
include("connection/config.php");
$sql = "select * from SUPPLIER_PROFILE ORDER BY SP_NAME";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<tr id='spvtr'>";
echo "<td id='spvtd' style='text-align:left;'>" . $row['SP_NAME'] . "</td>";
echo "<td id='spvtd'>" . $row['SP_TERMS_DAY'] . "</td>";
echo "<td id='spvtd'>" . $row['SP_VAT'] . "</td>";
echo "<td id='spvtd'>MODIFY SUPPLIER</td>";
echo "<td id='spvtd'>DELETE SUPPLIER</td>";
echo "</tr>";
}
?>
this is the addspform.php
<head>
<link rel="stylesheet" href="css/design.css" type="text/css" />
<link href="style.css" media="screen" rel="stylesheet" type="text/css" />
<link href="iconic.css" media="screen" rel="stylesheet" type="text/css" />
<script type = "text/javascript">
<!--
var run1 = function()
{
var x1 = document.addsp.spname.value.length;
if(x1 == 0)
{
alert("SUPPLIER NAME SHOULD NOT BE EMPTY");
return(false);
}
else
return(true);
}
var run2 = function()
{
var x1 = document.addsp.spadd.value.length;
if(x1 == 0)
{
alert("SUPPLIER ADDRESS SHOULD NOT BE EMPTY");
return(false);
}
else
return(true);
}
var run3 = function()
{
var x1 = document.addsp.sptd.value.length;
if(x1 == 0)
{
alert("SUPPLIER TERMS DAY SHOULD NOT BE EMPTY");
return(false);
}
else
return(true);
}
var run4 = function()
{
var x1 = document.addsp.spvat.value.length;
if(x1 == 0)
{
alert("SUPPLIER VAT SHOULD NOT BE EMPTY");
return(false);
}
else
return(true);
}
function checkall()
{
if(run1() == false)
{
addsp.spname.focus();
return(false);
}
if(run2() == false)
{
addsp.spadd.focus();
return(false);
}
if(run3() == false)
{
addsp.sptd.focus();
return(false);
}
if(run4() == false)
{
addsp.spvat.focus();
return(false);
}
if(/^[0-9]+$/i.test(addsp.sptd.value) == false)
{
alert("invalid Terms Day");
addsp.sptd.focus();
return(false);
}
else
{
return(true);
}
}
-->
</script>
</head>
<!-- body -->
<div id="content">
<div class="clear">
<p id="bcp">ADD Supplier</p><br><br>
<ul id="nav">
<li>
<a id="abutton" href="supplierprofile">VIEW SUPPLIER</a>
</li>
</ul>
<br>
<form id="aft" name ="addsp" action = "addsp.php" method = "post" onsubmit = "return checkall();">
<fieldset>
<legend>SUPPLIER FORM (NEW)</legend>
<table>
<tr><td>
<span style = "width: 50px;" >Supplier Name</span>
</td><td>
<input size="100" type = "text" name = "spname" onkeyup = "this.value = this.value.toUpperCase();" value =""/><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">Address</span>
</td><td>
<input size="100" type = "text" name = "spadd" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 120px;">Telephone No.</span>
</td><td>
<input size="100" type = "text" name = "sptelno" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">Terms Day</span>
</td><td>
<input size="100" type = "text" name = "sptd" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">VAT</span>
</td><td>
<select name="spvat">
<option id="o1"> </option>
<option id="o2">INCLUSIVE</option>
<option id="o3">EXCLUSIVE</option>
</select>
</td></tr>
<tr><td></td><td>
<input class="afbutton" type = "submit" name = "ADD" value = "ADD" id="ADD"/>
</td></tr>
<tr><td>
</table>
</fieldset>
</form>
</div>
</div>
<? -- script for pages --?>
<script type="text/javascript" src="js/general.js"></script>
<script src="js/submain.js"></script>
this is the addsp.php
<html>
<head>
<script type="text/javascript">
<!--
function direct()
{
window.alert("Suppler has been added");
w = window || document
w.location.reload();
}
-->
</script>
</head>
<body>
<?php
include("connection/config.php");
$spname = $_POST['spname'];
$spadd = $_POST['spadd'];
$sptelno = $_POST['sptelno'];
$sptd = $_POST['sptd'];
$spvat = $_POST['spvat'];
if(!$_POST['ADD'])
{
echo "Please fill out the form";
header('Location: addspform.php');
}
else
{
$query = "SELECT * FROM supplier_profile WHERE sp_name = '$spname'";
$result = mysql_query($query)or die(mysql_error());
$counter = 0;
while($supplier = mysql_fetch_array($result))
{
$counter = $counter + 1;
}
if($counter == 0)
{
mysql_query("INSERT INTO supplier_profile(`SP_NAME`,`SP_ADDRESS`, `SP_TELNO`,`SP_VAT`,`SP_TERMS_DAY`)
VALUES('$spname','$spadd','$sptelno','$spvat','$sptd')") or die(mysql_error());
echo '<script type="text/javascript">
direct();
</script>';
}
}
?>
</body>
sorry about the previous information. Now if i click the add button to the addspform.php all i want after the javascript that pop-up and click ok is to direct the supplierprofile.php inside in the main.php
make your addsp.php like this
<?php
session_start();
include("connection/config.php");
$spname = $_POST['spname'];
$spadd = $_POST['spadd'];
$sptelno = $_POST['sptelno'];
$sptd = $_POST['sptd'];
$spvat = $_POST['spvat'];
if(!$_POST['ADD'])
{
$_SESSION['msg'] = "Please fill form";
header('Location: addspform.php');
exit;
}
else
{
$query = "SELECT * FROM supplier_profile WHERE sp_name = '$spname'";
$result = mysql_query($query)or die(mysql_error());
$counter = 0;
while($supplier = mysql_fetch_array($result))
{
$counter = $counter + 1;
}
if($counter == 0)
{
mysql_query("INSERT INTO supplier_profile(`SP_NAME`,`SP_ADDRESS`, `SP_TELNO`,`SP_VAT`,`SP_TERMS_DAY`)
VALUES('$spname','$spadd','$sptelno','$spvat','$sptd')") or die(mysql_error());
$_SESSION['msg'] = "form has been submitted successfully";
header('Location: addspform.php');
exit;
}
}
?>
you can use header function of php to redirect on some page. use session as msg and print it on your form page as follows
<?php
session_start();
if(isset($_SESSION['msg']))
{
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form id="aft" name ="addsp" action = "addsp.php" method = "post" onsubmit = "return checkall();">
<fieldset>
<legend>SUPPLIER FORM (NEW)</legend>
<table>
<tr><td>
<span style = "width: 50px;" >Supplier Name</span>
</td><td>
<input size="100" type = "text" name = "spname" onkeyup = "this.value = this.value.toUpperCase();" value =""/><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">Address</span>
</td><td>
<input size="100" type = "text" name = "spadd" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 120px;">Telephone No.</span>
</td><td>
<input size="100" type = "text" name = "sptelno" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">Terms Day</span>
</td><td>
<input size="100" type = "text" name = "sptd" value = "" /><br />
</td></tr>
<tr><td>
<span style = "width: 100px;">VAT</span>
</td><td>
<select name="spvat">
<option id="o1"> </option>
<option id="o2">INCLUSIVE</option>
<option id="o3">EXCLUSIVE</option>
</select>
</td></tr>
<tr><td></td><td>
<input class="afbutton" type = "submit" name = "ADD" value = "ADD" id="ADD"/>
</td></tr>
<tr><td>
</table>
</fieldset>
</form>
redefine direct as:
function direct(){
window.alert("Suppler has been added");
w = window || document
w.location.reload();
}
Just do a redirect in addsp.php
if (test){
//do something
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=yourpage.php">';//This causes the browser to open the new page after 0 seconds, i.e immediately.
} else {
return false;
}
http://php.about.com/od/learnphp/ht/phpredirection.htm
//addsp.php
function direct()
{
window.alert("Suppler has been added");
document.location.href = "main.php?page='pagenamewhichlastloaded'&redirect=1";
}
//main.php
$(function(){
if($("#redirect").val() == 1){
var page = $("#page").val();
$('#content').load( page + '.php');
}
$('ul#nav li a').click(function() {
var page = $(this).attr('href');
$('#content').load( page + '.php');
return false;
});
});
<div id="content"></div>
<input type="hidden" id="page" value="<?php if(isset($_POST['page'])){ echo $_POST['page']; }?>" />
<input type="hidden" id="redirect" value="<?php if(isset($_POST['redirect'])){ echo $_POST['redirect']; }?>" />

Categories