How do I echo in php a jquery dialog box? - javascript

For a php project I am working on, I am trying to echo a popup window if an sql record is successful. I have been able to get this to work with javaScript alert but that’s not what I want, The JavaScript is preventing the rest of the page from loading until OK is clicked plus I would prefer to style a jQuery dialog instead.
Here is what does work in the php but not what I want:
$msg = "Success!";
if ($conn->query($sql) === TRUE) {
echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
I have found this below to prevent pop up until page loads, but it does not work:
echo '<script type="text/javascript">window.onload = function(){ alert("' . $msg . ' "));}</script>';
I am trying to get a jquery dialog to popup. Here is the basic jquery dialog from their website not working. It just displays the text in the paragraph tag. (I was planning on customizing it after its working):
echo '<script>
$(function() {
$( "#dialog" ).dialog();
});
</script>'
.'<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the x icon.</p>
</div>';

Thanks to user Wazani, I simply forgot to include the jquery UI library in addition to the core.

Related

Session cookie to play sound with Java Script just once

I encrypted a directory that contains several folders by directories Admin (Protect Option), for example,memberarea directory.
now if someone opens a page of these directories , he must enter his username and password so that he can see the contents of that directory.
I used the following code to write a welcome text to display the text related to each user. In addition, according to the entered username, an audio file will be played when the page open:
<?php
$user_name = $_SERVER['PHP_AUTH_USER'];
if ($user_name == "admin") {
$name = "Sir Alex Morgan";
echo '<div class="wellcome_message">' . $name . ' ، welcome to your site :)</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("admin_audio").play();
}
</script>';
}
elseif ($user_name == "user1") {
$name = "Miss Mary";
echo '<div class="wellcome_message">' . $name . ' ، nice to meet you</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("user1_audio").play();
}
</script>';
}
.
.
.
?>
Now I want to make the above process happen only once in each browser session, ie if the user, for example, went from page domain.com/memberarea/part1/index.php to page domain.com/memberarea/part3/page.php just as he does not need to enter the password again until the browser is closed, The welcome message and voice will only be displayed for the first time.

javascript alert box not showing on php after header redirect

I am trying to display an alert box before redirecting to another page, here is my code, when I remove the header function it works properly but when it is here it will just redirect to the page without showing the alert box.
<html>
<body>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig, $_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig, $sql_query);
if ($result) {
echo '<script language="javascript">';
echo 'alert("visit deleted successfully")';
echo '</script>';
header("location:../SearchCountry/search.php");
}
?>
</body>
</html>
PHP is executed at the server side. It renders HTML/JS/CSS and sends it to the web browser, the web browser then parses and executes the JavaScript (In your case, show the alert dialog.)
However, once you call
header ("location:../SearchCountry/search.php");
The browser will be informed to redirect the user to ../SearchCountry/search.php immediately, without a chance to parse and execute the JavaScript. That's why the dialog will not show up.
Solution: redirect your user to another page with JavaScript instead of PHP.
<html>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig,$_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig,$sql_query);
if($result){
echo '<script language="javascript">';
echo 'alert("visit deleted successfully");\n';
echo 'window.location.href="../SearchCountry/search.php"'; //Redirects the user with JavaScript
echo '</script>';
die(); //Stops PHP from further execution
}
?>
</body>
</html>
echo "<script>
alert('visit deleted successfully');
window.location.href='SearchCountry/search.php';
</script>";
and get rid of redirect line below.
You were mixing up two different worlds.

Creating a rollover with PHP and Javascript

I've read a TON of question and answers about rollovers using PHP and Javascript and can't figure out why this isn't working. So I have this PHP code:
$rollover = '$("#' . $godName . '").mouseenter(function() {
$("#' . $godName . '").attr("src","img/gods/god_cards/dark/' . $godImage . '2.png");
});
$("#' . $godName . '").mouseleave(function() {
$("#' . $godName . '").attr("src","img/gods/god_cards/' . $godImage . '.png");
});';
$godName and $godImage are variables retrieved through a query to a database. I wanted to use this code to cut down on the amount I'd have to write to create a rollover for over 50 images, so basically it would create a function for every image retrieved in the query. Then I wanted to stick it in a javascript tag like so:
<script type="text/javascript">
<?php echo $rollover; ?>
</script>
But it doesn't work. And I've tried doing it with and without the echo and no version of whatever I've found on here or anywhere else works. When I echo it out as just text it looks fine, it spits out what it should so I'm not sure why the javascript just doesn't seem to be accepting the php. Any help?
Do you check if the HTML Dom is ready? You are using jQuery, so you can use:
<script type="text/javascript">
$(function(){
<?php echo $rollover; ?>
});
</script>

Show a timed message between submit and reload

I have a "Bootstrap" form. When the user press "Submit" data is sent to file.php If the database is updated, I have an alert in that php file which echos an alert with "success". Now I'd like to alert a timed message. Not the alertbox with "OK" button. Is there some small simple code for this?
This is what I have now!
PHP
//Echo succes
echo "<script type='text/javascript'>";
echo "alert('Välkommen ".$row['usr_fname']." ".$row['usr_lname']."');";
echo 'window.location = "../back_to_form.html"';
echo "</script>";
die();
You could potentially use something like Jquery UI to fake a dialog box. Then the reloading could happen just like you want it to. The code would look something like this (untested, you'd need to adapt it to your app):
echo "<html><body>";
echo '<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>';
echo "<div id="dialog" title="Basic dialog">";
echo "<p>Välkommen ".$row['usr_fname']." ".$row['usr_lname']."</p>";
echo "</div>";
echo "<script type='text/javascript'>window.settimeout(window.reload(), 5000)</script>";
echo "</body></html>";
More info with a better javascript example for the jquery ui message box can be found here

Embedding multiline alert box in php code

I would like to embed a JavaScript alert box into php code and would like the text to report variable values. The following code gives multiline but does not enable me to use variables.
?>
<script type="text/javascript">
alert("Total size of all files too large.\nConsider uploading files in smaller sets.\nYou can append to existing sets.");
document.location.href="SomewhereElse.php";
</script>
<?php
The following code lets me use variables but does not give multiline.
$Str="Total size cannot be not more than " . ini_get('post_max_size');
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
When I try this code, no alert box comes up.
$Str="Unable to load (all of) these files because of their (total) size" .
"\nPlease make the upload set conform to the following parameters";
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
I do get an alert box if I leave the \n (before the Please) out but it is not multiline. I am wondering if the \n dismisses the alert box. At any rate, how do I get a multiline alert box with variables?
try something like this:
<?php
$max_size = ini_get('post_max_size');
echo '<script>';
echo 'alert("Total size cannot be more than ' . $max_size . '!\nPlease make sure...");';
echo '</script>';
?>

Categories