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']; }?>" />
Related
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.
I've build a function in javascript to show me a second submit but only after the first submit was clicked.
Edited - this is ex.php:
<form method="post" action = "ex.php">
<input type="submit" id="button" name="search" value="Search"
onclick="myFunction();" >
<br>
<input type="submit" name="search_close" id="submit" style="display:
none;" value="Find close results">
</form>
<?php
if(isset($_POST['search']))
{
echo "first search";
}
else if(isset($_POST['search_close']))
{
echo "second search";
}
else {
echo "nothing";
}
?>
<script type="text/javascript">
inputSubmit = document.getElementById("submit");
function myFunction(){
inputSubmit.style.display = "block"; };
</script>
Updated:
I've edited in order to conclude the main problem.
So what I want is this:
a) The user has pressed the first submit button "search" then it will echo on the page "first search" and then show the second submit button "search_close" .
b)Then, if the user has pressed the second submit button it will show "second search".
When first Refreshing:
======
search (button)
======
if clicking the "search" button:
======
search (button)
======
first search (text)
==================
Find close results (button)
==================
if clicking the "find close results:
======
search (button)
======
first search (text)
==================
Find close results (button)
==================
second search (text)
This code doesn't do what I want. Why?
Updated - why the button find close results disappers after one second?
<?php
session_start();
$db=mysqli_connect("localhost","root","","travelersdb");
if(#$_SESSION["username"]){
?>
<?php
// function to connect and execute the query
function filterTable($query)
{
$connect=mysqli_connect("localhost","root","","travelersdb");
$filter_Result = mysqli_query($connect, $query) or die(mysqli_error($connect));
return $filter_Result;
}
$submit_value = 0;
if(isset($_POST['search']))
{
$Destination = $_POST['Destination'];
$TypeOfTravel = $_POST['TypeOfTravel'];
$Age= $_POST['Age'];
$email = $_POST['email'];
$topic_name = $_POST['topic_name'];
$StartingPoint = $_POST['StartingPoint'];
// search in all table columns
$query = "SELECT * FROM `topics`
left join `users` on users.username = topics.topic_creator
WHERE 1=1 ";
if(!empty($Destination)) {
$query.="AND destination='$Destination'";
}
if(!empty($TypeOfTravel)) {
$query.=" AND typeTravel='$TypeOfTravel'";
}
if(!empty($Age)) {
$query.="AND age='$Age'";
}
if(!empty($email)) {
$query.=" AND email='$email'";
}
if(!empty($topic_name)) {
$query.=" AND topic_name='$topic_name'";
}
if(!empty($StartingPoint)) {
$query.=" AND StartingPoint='$StartingPoint'";
}
$search_result = filterTable($query);
$submit_value = 1;
}
///Make The search more wider, only for the fields that were in the post
else if(isset($_POST['search_close']))
{
$Destination = $_POST['Destination'];
$TypeOfTravel = $_POST['TypeOfTravel'];
$topic_name = $_POST['topic_name'];
$StartingPoint = $_POST['StartingPoint'];
// search in all table columns
$query = "SELECT * FROM `topics`
left join `users` on users.username = topics.topic_creator
WHERE 1=1 ";
if(!empty($Destination)) {
$query.="AND destination='$Destination'";
}
if(!empty($TypeOfTravel)) {
$query.=" AND typeTravel='$TypeOfTravel'";
}
if(!empty($topic_name)) {
$query.=" AND topic_name='$topic_name'";
}
if(!empty($StartingPoint)) {
$query.=" AND StartingPoint='$StartingPoint'";
}
$search_result = filterTable($query);
$submit_value = 2;
}
else {
$query = "SELECT * FROM `topics`";
$search_result = filterTable($query);
}
?>
<html>
<head>
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Home Page</title>
<style>
.hidden {
display: none;
}
</style>
<script type="text/javascript">
function showHide()
{
var checkbox = document.getElementById("chk");
var hiddeninputs = document.getElementsByClassName("hidden");
for (var i=0; i != hiddeninputs.length; i++) {
if(checkbox.checked){
hiddeninputs[i].style.display = "block";
} else {
hiddeninputs[i].style.display = "none";
}
}
}
<?php
if($submit_value == 1 || $submit_value == 2){ ?>
myFunction();
inputSubmit = document.getElementById("submit");
function myFunction(){
document.getElementById('submit').style.display = "block";
};
<?php } ?>
</script>
<body>
</body>
</head>
<?php include("header.php");?>
<center>
</br>
<button>Post</button>
</br>
<br/>
<h4>Simple Serach</h4>
<form action="" method="post">
<input type="text" name="Destination" placeholder="Destination" value=
"<?php if(isset($_POST['Destination']))
{echo htmlentities($_POST['Destination']);}?>" ></br>
<input type="text" name="TypeOfTravel" placeholder="Type Of Travel"
value=
"<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['TypeOfTravel']);}?>"
></br>
<input type="text" name="Age" placeholder="Age" value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['Age']);}?>">
</br>
</br>
<!-- Advanced Search-->
<input type="checkbox" name="chkbox" id="chk" onclick="showHide()" />
<label for="chk"><b>Advanced search</b></label>
</br>
<input type ="text" name="email" placeholder="Email" class="hidden" value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['email']);}?>">
<input type ="text" name="topic_name" placeholder="topic name" class="hidden"value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['topic_name']);}?>">
<input type="text" name="StartingPoint" placeholder="Starting Point" class="hidden"value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['StartingPoint']);}?>">
</br><br/>
<input type="submit" id="button" name="search" value="Search"
onclick="myFunction();" >
<br><br>
<input type="submit" name="search_close" id="submit" style="display:
none;" value="Find close results">
</form>
<br/>
<?php echo '<table border="1px">';?>
<td width="400px;" style="text-align:center;">
Name
</td>
<td width="400px;" style="text-align:center;">
Destination
</td>
<td width="400px;" style="text-align:center;">
Type Of Travel:
</td>
<td width="80px;" style="text-align: center;">
First Name
</td>
<td width="80px;" style="text-align: center;">
Age
</td>
<td width="400px;" style="text-align:center;">
profile picture
</td>
<td width="80px;" style="text-align: center;">
Creator
</td>
<td width="80px;" style="text-align: center;">
Date
</td>
</tr>
</center>
<?php
$sql = "Select * from `topics`";
$check = mysqli_query($db,$sql);
$rows = mysqli_num_rows($search_result);
if($rows != 0){
while ($row = mysqli_fetch_assoc($search_result)){
$id = $row['topic_id'];
echo "<tr>";
//echo "<td>".$row['topic_id']."</td>";
echo "<td style='text-align:center;'><a href='topic.php?id=$id'>".$row['topic_name']."</a></td>";
echo "<td>".$row['Destination']."</td>";
echo "<td>".$row['typeTravel']."</td>";
$sql_u = "Select * from users where username='".$row['topic_creator']."'";
$check_u = mysqli_query($db,$sql_u);
while ($row_u = mysqli_fetch_assoc($check_u))
{
$user_id = $row_u['id'];
$profile_pic = $row_u['profile_pic'];
$firstname = $row_u['firstname'];
$age = $row_u['age'];
echo "<td>".$firstname."</td>";
echo "<td>".$age."</td>";
echo "<td><img src='".$profile_pic."' width='10%' height='10%' alt='me'></td>";
echo "<td><a href='profile.php?id=$user_id'>".$row['topic_creator']."</a></td>";
}
$get_date = $row['date'];
echo "<td>".$row['date']."</td>";
echo "</tr>";
}
}else {
echo "No topics found";
}
echo "</table>";
if (#$_GET['action']=="logout")
{
session_destroy();
header('location:login.php');
}
}else
{
echo "You must be logged in.";
echo "<a href ='login.php'>Click here to login</a>";
}
?>
</br>
</br>
<body>
</body>
</html>
you need to set type button cause when you set type submit then form automatically submit so show second not show but it work.
<form method="post" action = "">
<input type="submit" id="button" name="search" value="Search" >
<br>
<input type="submit" name="search_close" id="submit" style="display:
none;" value="Find close results">
</form>
<?php
$submit_value = 0;
if(isset($_POST['search']))
{
echo "first search";
$submit_value = 1;
}
else if(isset($_POST['search_close']))
{
echo "first search";
echo '<br>';
echo "second search";
$submit_value = 2;
}
else {
echo "nothing";
}
?>
<script type="text/javascript">
<?php
if($submit_value == 1 || $submit_value == 2){ ?>
document.getElementById('submit').style.display = "block";
<?php } ?>
</script>
check this code
I have cleaned up your code, but the issue of having two submit buttons still persists. Even if the clicking of the first button causes the second to show, the first button will cause the page to reload with the results from the form's action resource. In this example, I have cancelled the form's submit event to show that the display of the second button works.
In the end, I think you are going about this all wrong. I think you should be making AJAX calls to your server-side resource and injecting the results of that call into your page - - no forms and no submits.
// Don't forget to add "var" to your variable declarations, otherwise the variables
// become global!
var sub1 = document.getElementById("btnSub1");
var sub2 = document.getElementById("btnSub2");
// Set up the event handler for the first button
sub1.addEventListener("click", myFunction);
function myFunction(e){
// Adjust classes, not individual styles
sub2.classList.remove("hidden");
// Cancel the native event and stop bubbling
e.preventDefault();
e.stopPropagation();
};
.hidden { display:none; }
<form>
<!-- Submit buttons don't get a name attriute unless you expect their value to be submitted
along with all the other form data. Inline styles should be avoided and internal style
sheets used instead. And, inline JavaScript event attributes (onclick, etc.) should not
be used. -->
<input type="submit" id="btnSub1" name="search" value="Search" >
<br>
<input type="submit" id="btnSub2" class="hidden" value="Find close results">
</form>
<?php
if(isset($_POST['search']))
{
echo "first search";
}
else if(isset($_POST['search_close']))
{
echo "second search";
}
else {
echo "nothing";
}
?>
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>
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>';
}
I have a site I'm working on Modestewebstudio.com
On the contact section of the site I have a form that I'm trying to submit using jQuery and pHp. For some reason the form does not submit. The validation works fine but whenever I hit submit nothing happens.
$(document).ready(function () {
$(window).load(function() { $("#load").fadeOut("slow"); });
var fadeInElement = function(id) {
$('#' + id).fadeIn();
};
//HOME-------------------------------------------------
$(".home").click(function () {
$('#about, #works, #contact').filter(":visible").fadeOut();
fadeInElement('home');
});
//ABOUT------------------------------------------------
$(".about").click(function () {
$('#home, #works, #contact').filter(":visible").fadeOut();
fadeInElement('about');
});
//WORKS------------------------------------------------
$(".works").click(function () {
$('#home, #about, #contact').filter(":visible").fadeOut();
fadeInElement('works');
});
//CONTACT----------------------------------------------
$(".contact").click(function () {
$('#home, #about, #works').filter(":visible").fadeOut();
fadeInElement('contact');
});
<!--//--><![CDATA[//><!--
$('form#contact-us').submit(function() {
$('form#contact-us .error').remove();
var hasError = false;
$('.requiredField').each(function() {
if($.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">Your '+labelText+' is missing.</span>');
$(this).addClass('inputError');
hasError = true;
} else if($(this).hasClass('email')) {
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test($.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">Your '+labelText+' is invalid.</span>');
$(this).addClass('inputError');
hasError = true;
}
}
});
if(!hasError) {
var formInput = $(this).serialize();
$.post($(this).attr('action'),formInput, function(data){
$('form#contact-us').slideUp("fast", function() {
$(this).before('<p>Your email has been delivered!</p>');
});
});
}
return false;
});
//-->!]]>
//-----------------------------------------------------
});
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submitted'])) {
// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'Forgot your name!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$emailTo = 'zenneson#gmail.com';
$subject = 'Submitted message from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Denneson Modeste</title>
<!--Fonts-->
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Syncopate' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Special+Elite' rel='stylesheet' type='text/css'>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="load">
<div class="load-logo pulse"></div>
<p>Loading</p>
</div>
<div class="tv"></div>
<div class="home tv-btn">HOME</div>
<div class="about tv-btn">ABOUT</div>
<div class="works tv-btn">WORKS</div>
<div class="blog tv-btn">BLOG</div>
<div class="contact tv-btn">CONTACT</div>
<div id="home" class="channel">
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/JdRO97mFMx8?rel=0&controls=0&autoplay=1&showinfo=0&disablekb=1&disablekb=1&modestbranding=1&start=7&loop=1&playlist=JdRO97mFMx8&wmode=opaque" frameborder="0" allowfullscreen></iframe>
</div><!-- end of home -->
<div id="about" class="channel">
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/JdRO97mFMx8?rel=0&controls=0&autoplay=1&showinfo=0&disablekb=1&disablekb=1&modestbranding=1&start=7&loop=1&playlist=JdRO97mFMx8&wmode=opaque" frameborder="0" allowfullscreen></iframe>
</div><!-- end of about -->
<div id="works" class="channel">
</div><!-- end of works -->
<div id="contact" class="channel">
<div class="contact-border"></div>
<div class="stamp"></div>
<div class="contact-section">
<h3>Have a question or wanna say hello? Leave a message below.</h3>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<p class="info">Your message was sent and will be responded to ASAP.</p>
<?php } else { ?>
<form id="contact-us" action="contact.php" method="post">
<?php if(isset($hasError) || isset($captchaError) ) { ?>
<p class="alert">There was a mistake in the message</p>
<?php } ?>
<label>Name</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="Name:" />
<?php if($nameError != '') { ?>
<br /><span class="error"><?php echo $nameError;?></span>
<?php } ?>
<label>E-mail</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="txt requiredField email" placeholder="Email:" />
<?php if($emailError != '') { ?>
<br /><span class="error"><?php echo $emailError;?></span>
<?php } ?>
<label>Message</label>
<textarea name="comments" id="commentsText" class="txtarea requiredField" placeholder="Message:"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<br /><span class="error"><?php echo $commentError;?></span>
<?php } ?>
<button name="submit" type="submit" class="subbutton"> </button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
</div><!-- end of contact-section -->
<?php } ?>
</div>
</div>
</div><!-- end of contact -->
<!--Javascript-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/application.js"></script>
</body>
</html>
Submitting the form results in the following error
404 (Not Found) http://modestewebstudio.com/contact.php
Make sure there that the file is in fact named contact.php, and that it's placed in the root directory of your web server.