Embedding multiline alert box in php code - javascript

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>';
?>

Related

How to know system command is in use in php?

I have a HTML/PHP code as shown below in which on click of a button conversion of mp4 into mp3 starts happening.
HTML/PHP Code:
<?php foreach ($programs as $key => $program) { ?>
<tr data-index="<?php echo $key; ?>">
<td><input type="submit" id="go-btn" name="go-button" value="Go" data-id="<?php echo $key; ?>" ></input></td>
</tr>
<?php }?>
Php code (where mp4=>mp3 conversion happens):
$f = $mp4_files[$_POST['id']];
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
system('C:\ffmpeg\bin\ffmpeg.exe -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
break;
}
As soon as the button is clicked from the HTML/PHP Code above, the text gets changed from Go to Converting in the UI because I have added JS/jQuery code in my codebase but this JS/jQuery code which I have added just change the text only.
It doesn't actually know that the Conversion is happening in the background.
JS/jQuery code:
$("input[name='go-button']").click( function() {
// Change the text of the button, and disable
$(this).val("Converting").attr("disabled", "true");
});
Problem Statement:
I am wondering what modification I need to do in the JS/jQuery code above so that UI actually knows that conversion is happening in the background.
Probably, we need to add make establish some connection between JS/jQuery and php code above but I am not sure how we can do that.

Displaying a number from the database in a javascript alert box

The following code is to used to display an alert box with the order number when a html form is submitted successfully!
$mysql="SELECT MAX(OrderNo.) FROM `order` ";
$results=mysqli_query($db,$mysql);
$row=mysqli_fetch_array($results);
echo '<script type="text/javascript">';
echo 'alert("Successful signup your order number is"';
echo $row['OrderNo.'];
echo ")";
echo '</script>';
Though the alert box appears with "Successful signup your order number is" part, the OrderNo. part(retrieved from a databse table) does not appear in the alert box! Here max is used because the latest orderNo. is to be displayed and the OrderNo. field is auto incremented.
please help me to correct this error
You are not closing double quotes correctly.
It should be:
echo 'alert("Successful signup your order number is ';
echo $row['OrderNo.'];
echo '")';
Or better:
echo 'alert("Successful signup your order number is '.$row['OrderNo.'].'")';
Try to create an alias for the grouped number like this:
SELECT MAX(OrderNo) AS number FROM order
And show with:
echo 'alert("Successful signup your order number is' . $row['number'] . '")';

javascript alert and then a location replace not working

I trying to use an alert message when someone enters incorrect login details, and I then want to redirect them back to the same login page. I'm doing this inside a php file that is called when someone submits a login form. I'm trying something like this:
echo '<script language="javascript">';
echo 'alert("Invalid Username or Password")';
echo 'location.replace("target.php")';
echo '</script>';
but it goes to the php file itself, which shows nothing.
update it you have missing ; for alert
echo '<script language="javascript">';
echo 'alert("Invalid Username or Password");';
echo 'window.location("target.php")';
echo '</script>';
echo '<script>';
echo 'alert("Invalid Username or Password"); ';
echo 'location.replace("http://mywebsite.com/target.php"); ';
echo '</script>';
language attribute is deprecated
location.replace("target.php") must have valid url: location.replace("http://mywebsite.com/target.php") or you can use history.back()
and as noted, don't miss semicolon

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

Javascript in php not working

I'm trying to make the page go back when the user presses the OK button on the alert box. The alert box appears but it doesn't go back to the previous page
Would i have to use a confirm window because i don't want to provide the cancel option, i just want a window to appear so the user has to click OK and it goes back to the previous page
print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
Just to provide a little more information I'm doing this after OR
$link =mysqli_connect(*sensitive info*) OR print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
exit();
Try this, You have missed to add ;
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
.........^
print 'history.back();';
print '</script>';
I think you're missing a semicolon ; after alert().
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
You have missed the ';' after your alert
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
To find our errors in code you can use websites such as http://www.jslint.com/ to catch these errors easily.
echo "<script type=\"text/javascript\">";
echo "alert(\"Invalid please try again.\")";
echo "window.location= \"/*Write ur page Link Here*/\"";
echo "</script>";
Hope above code will work for you. and yeah ";" is missing after your alert line.

Categories