What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/x-javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
My problem is the javascript function does not execute after the php updtates the database.
I appreciate any advice or comments you have about this.
The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">
You can do the following:
Move function up and fix x-javascript:
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
Fiddle: Fiddle
You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.
Heres a jquery example:
$.post('/myscript.php', {foo: 'bar'}).done(function (response) {
window.open(......);
});
I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.
echo "<script> window.onload=confirmFunction(); </script>";
Hope this what you are looking for.
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/javascript">
window.onload = function(){
<?php
if(submitted){
echo "confirmFunction();";
}
?>
}
</script>
Try:
if (r==true) {
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
window.location = "PAGE THAT YOU WANT";
}
Related
I have this code that deletes a student record from the database, it deletes the student information as well as where an image is stored on the server however, I also want it to delete the image file itself, not just the record. How can I do this.
This code is where the user clicks to delete the student.
<a href="javascript:void();" onclick="deleteItem
(<?php echo $student_row->id;?>,<?php echo $_REQUEST['regNumber'];?>,
<?php echo $student_row->id;?>,'parent')">Delete</a>
This code is the JS code referenced above:
function deleteItem(registration_number,parent_id,id,type)
{
var parent_id = "<?php echo $_REQUEST['regNumber'];?>";
var url_pass="<?php echo get_site_url();?>/student-delete/?
regNoIndivid="+registration_number+
"&parentId="+parent_id+"&id="+id+"&type="+type;
if (confirm("Are you sure?")) {
window.location.href = url_pass;
}
return false;
}
This is from student-delete:
if($_REQUEST['type'] == "teacher")
{
$where=array("id"=>$_REQUEST['id']);
//echo "<pre>";print_r($where);echo "</pre>";
//$delete_row=$wpdb->delete('wp_new_student_user', $where);
$delete_row = $wpdb->query('DELETE FROM wp_new_student_user WHERE id
='.$_REQUEST['id']);
$wpdb->show_errors();
if($delete_row)
{
$url_location=get_site_url()."/my-account-teacher/?
regNumber=".$_REQUEST['parentId']."&type=tea&back_list=yes";
?>
<script type="text/javascript">
window.location.href="<?php echo $url_location;?>";
</script>
<?php
}
}
elseif ($_REQUEST['type'] == "parent") {
$where=array("id"=>$_REQUEST['id']);
//echo "<pre>";print_r($where);echo "</pre>";
//$delete_row=$wpdb->delete('wp_new_student_user', $where);
$delete_row = $wpdb->query('DELETE FROM wp_new_student_user WHERE id
='.$_REQUEST['id']);
$wpdb->show_errors();
if($delete_row)
{
$url_location=get_site_url()."/my-account-parent/?
regNumber=".$_REQUEST['parentId']."&type=par&back_list=yes";
?>
<script type="text/javascript">
window.location.href="<?php echo $url_location;?>";
</script>
<?php
}
}
Please use PHP UNLINK function to delete the file
if($delete_row)
{
unlink(Path to the file);
}
I'm currently attempting to create a test-website based on the "Secret Diary" project of a web developer course. I'm trying to create a page that saves all of the notes written into a textbox, and displays them when I log in again. Almost everything works - I can start a session and display the saved text when I log in, but the box is deleting the textbox's saved data when the page is loaded. I know that there are better ways of storing the info, I'm just looking for how to get this method to work. This should be all of the relevant code:
mainpage.php:
<?php include("updatediary.php"); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<form method="post">
<textarea class="form-control"><?php echo $diary; ?></textarea>
</form>
<script>
$("textarea").keyup(function() {
$.post("updatediary.php", function(){diaryInput:($("textarea").val());} );
});
</script>
updatediary.php:
<?php
include("connection.php");
$query = "SELECT content FROM users WHERE id='".$_SESSION['id']."' LIMIT 1";
$result = mysqli_query($dbCon,$query);
$row = mysqli_fetch_array($result);
$diary = $row['content'];
if ($_POST['diaryInput']!="") {
$updateQuery = "UPDATE `users` SET `content`='".mysqli_real_escape_string($dbCon, $_POST['diaryInput'])."' WHERE id='".$_SESSION['id']."' LIMIT 1";
if (mysqli_query($dbCon, $updateQuery)) {
echo "saved";
} else {
echo "not saved";
};
}
?>
connection.php:
$dbCon = mysqli_connect("localhost", "owenxwfg_admin", "(password)", "owenxwfg_users");
Any help would be awesome. I personally think that there's a problem with my $.post part.
I've create a code that check if a variable is empty or not.
If the variable is empty I execute a javascript alert, in particular:
if($verbo_name == NULL)
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
If I insert a message inside the echo, the message appears correctly, but I want show the alert in javascript. What's the error? Thanks..
UPDATE more details:
$results = $con->query("SELECT verbo, descrizione FROM verbo WHERE verbo = '$verbo'");
$verbo_name = NULL;
while($row = $results->fetch_array())
{
$verbo_name = $row['verbo'];
}
Check below codes are working fine for me.
<?php
$verbo_name = NULL;
if(is_null($verbo_name))
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
?>
Check this one
if(empty($verbo_name))
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
This should trigger an alert:
<html>
<body>
<?php
if($verbo_name == NULL)
{
$msg = enter code here'no record available in the database';
} else {
$msg = 'not set';
}
echo "
<script>
alert('". $msg ."');
</script>";
?>
</body>
</html>
If the message is not set the problem is just the variable.
This is Pravat Kumar Sahoo's answer,
This must work fine.
If you are testing on google chrome, you might have prevented the alert dialog message mistakenly. Please try clearing the cache/cookie or test on any other browser.
<?php
$verbo_name = NULL;
if(is_null($verbo_name))
{
echo "<script>alert('no record available in the database');</script>";
exit();
}
?>
Update:
Instead of alerting the message inside PHP, echo a message, like "NoData". And in the ajax calling Javascript, receive response "success".
echo "NoData"; // In PHP file.
In your Javascript, get the response,
success:function(response)
{
if(response == "NoData");
{
alert("No data in the Database");
}
}
I'm working on a login page by user level to separate the admin and user. but it didnt seems to work. it doesnt redirect and leave a blank page. I've tried remove the javascript part, but it doesnt change anything either.
index.php
<form class="login" action="login.php" method="post">
Username:<input type="text" name="username" id="username"/>
Password:<input type="password" name="password" id="password"/>
<input type="submit" value="login"/>
</form>
login.php
<?php
session_start();
include('config.php');
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql = mysql_query("SELECT * FROM admin WHERE username='$username' AND password='$password'");
$result = mysql_fetch_array($sql);
$username=$result['username'];
$adminID=$result['adminID'];
$userLevel=$result['UserLevel'];
$_SESSION['adminID']=$adminID;
$_SESSION['userLevel']=$userLevel;
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if($userLevel == '1')
{
$sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
$result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
?>
<script type="text/javascript">
alert("Welcome <?php echo "$username" ?> to Admin page! ");
</script>
<?php
header('Location:admin.php');
exit();
}
elseif($userLevel == '0')
{
$sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
$result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
?>
<script type="text/javascript">
alert("Welcome <?php echo "$username" ?> to User page! ");
</script>
<?php
header('Location: user.php');
exit();
}
else
{
?>
<script type="text/javascript">
alert("Invalid Username or Password! ");
//window.location.href = "index.php";
</script>
<?php
}
}
?>
Use PHP Header:
for userLevel1:
header("Location: admin.php");
for userLevel2:
header("Location: user.php");
Name in your submit so it will enter your PHP code block:
<input type="submit" name="submit" value="login"/>
try the following code and replace into your code. see whether can work or not. you try on the first if condition first and see on the result. if cannot work tell me what problem you face.
<?php
if($userLevel == '1')
$sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
$result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
?>
<script>
var a = alert("Welcome <?php echo "$username" ?> to Admin page! ");
if (a === true){
window.location.href="admin.php";
}
else{
window.location.href="admin.php";
}
</script>
<?php
}
I want to display a javascript alert box in php and navigate back to the main page. Here is my code
session_start();
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">alert(" You are not allowed to view this contnent"); </script>';
header('Location: mainPage.php');
}
//the rest of my page
With this code the alert popup is not displayed only the redirection. I also try this
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.navigate("mainPage.php"); </script>';
}
//the rest of my page
Now the popup is displayed but the redirection does not work.
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.location.href="mainPage.php"; </script>';
}
please review this code
session_start();
$name = $_SESSION['name'];
$id = $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">
alert(" You are not allowed to view this contnent");
self.location="mainPage.php";
</script>';
}
Try this
try this.Just add after how many seconds the page need to redirect . so
change
header('Location: mainPage.php');
to
header( "refresh:5; url=mainPage.php" );