cant echo javascript alert from php - javascript

I've looked for an answer already, but I understand how to achieve an alert from php I just don't know what I am doing wrong on this particular piece of code.
I had this working until I added the if statement.
if ($errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
It worked fine when it was just .
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
If comment everything out and just do
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
this will not work. I'm so confused. It doesn't make sense why the second alert doesn't work.
I forgot to mention, In the if statement from above the first alert will work, it's that second alert that I can't get to fire.
Sorry about the confusion, the $error is a bool. If it is true a filw was uploaded, if false it wasn't.

I think you are doing the opposite of what you want:
Your if case doesn't match the action performed inside.
Maybe you should do:
if (!$errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
and you can also save up on echo words - and make it more readable - doing sth like:
echo <<<HTML
<script type='text/javascript'>
alert('Records Were Uploaded');
window.location.href = 'EmployeePicker.php';
</script>
HTML;
just if you want :)
[EDIT] Note that according to what $errors hold (either integer or array), you can check that there is no error by either !$errors if integer or !count($errors) if array.
[EDIT] Trying your second piece of code as a standalone
If you need to trace what is wrong you need to isolate bit by bit!
first try code 1 as a new php file if it does not alert and redirect on your system then something is wrong in your configs.
If it works something is wrong in your code logic.
I could also run code 2 with no pb by first setting the $errors var to 1 or 0.
code 1
<?php
$errors = 1;
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
?>
code 2
<?php
$errors = 1;
if ($errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
// echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
// echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
?>
And about your $errors var, if true means no error you should definitely rename it to sth less tricky like $uploadSuccessful. ;)

It should echo out good. Try checking your php error log file to see if there are any clues as to the issue in there.
Just a small most like likely personal preference of mine that may help you is to separate out the logic a bit. Please see below.
<?php
// STATUS
$errors = true;
// OUTPUTS
$upload_success = "<script type='text/javascript'>";
$upload_success .= "alert('Records Were Uploaded');";
$upload_success .= "window.location.href = 'EmployeePicker.php';";
$upload_success .= "</script>";
$upload_fail = "<script type='text/javascript'>";
$upload_fail .= "alert('There was a problem with your file');";
$upload_fail .= "window.location.href = 'csvUpload.php';";
$upload_fail .= "</script>";
//LOGIC
if ($errors) {
echo $upload_success;
} else {
echo $upload_fail;
}
?>

Related

How to call bootstrap modal in php

I'm trying to call a modal that I have in a html file which is hidden alongside another modal. One of them appears as the button is clicked but the other one is to be called by php as part of a verification process.
Here is my php code:
$File = include("index2.html");
$Msg = 'Welcome ';
$search=$_POST['search'];
$query = $pdo->prepare("SELECT * FROM students WHERE IDs LIKE '%$search%' LIMIT 0 , 10"); //OR author LIKE '%$search%'
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo '<script type="text/javascript">';
echo 'alert("Verification Complete");';
echo '</script>';
while ($results = $query->fetch()) {
$Studname = $results['Studentname'];
echo '<script type="text/javascript">';
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';
echo '<script type="text/javascript">';
echo '$(File).ready(function() {';
echo '$("#myAdvert").modal("show");';
echo '});';
echo '</script>';
}
} else {
echo '<script language="javascript">';
echo 'alert("Sorry your ID was not found");';
echo 'window.location.href = "/Koleesy/index.html";';
echo '</script>';
}
?>
After the user is welcomed I want the hidden modal from my index.html to be called so the user can continue submitting the advert.
I've tried using the include function but that doesn't seem to work in my favor.
How do I do this?
Every answer is appreciated.
Try this:
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#myAdvert').modal('show');
});
</script>";

How to retrieve $_SESSION value in a JavaScript/HTML

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.

show and hide elements using mysql enum and javascript in php?

I am trying to simply show and hide an element depending on the value of an ENUM in mysql database.
I am using the javascript and PHP but it seems like the javascript within PHP does not or cannot select element given as the element is always on display!
here is my php code:
if ($sizeSelect != '1') {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}
and this is my HTML element:
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
is there anything that I'm missing?
any help would be appreciated.
Thanks
P.S. I have made sure that I am connected to mysql database and get the ENUM value properly so the mysql connection is not an issue here.
okay, I just did a test within my html file and placed the following code at the top of the page and still didn't work but when I put the same code at the bottom of the page, it did work and changed the element's display to none:
<script language='javascript' type='text/javascript'>
document.getElementById('sizeSelect').style.display = 'none';
</script>
SO, i did try this code in my PHP file with document ready function but still doesn't work from php file!
if ($sizeSelect = 1) {
echo "<script language='javascript' type='text/javascript'>";
echo
"$(document).ready(function(){ document.getElementById('sizeSelect').style.display = 'block';});";
echo "</script>";
}else{
echo "<script language='javascript' type='text/javascript'>";
echo "document.getElementById('sizeSelect').style.display = 'none';";
echo "</script>";
}
any help would be great.
Try to put your php code that generating the javascript after your SELECT tag or at the end of file, because your javascript doesn't find the DOM element "#sizeSelect", i made test and it worked for me. like this:
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
<?php
$sizeSelect = // retrieve sizeSelect value from your database;
if ($sizeSelect != '1') {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}
?>
After you specified that you're using Smarty, i come back with a solution for that:
Concatenate your js content produced in php file on a string variable instead of using echo, and assign its value with Smarty assign() function
$jsContent= '';
if ($sizeSelect != '1') {
$jsContent.= '<script type="text/javascript">';
$jsContent.= 'document.getElementById("sizeSelect").style.display = "block"';
$jsContent.= '</script>';
}
else{
$jsContent.= '<script type="text/javascript">';
$jsContent.= 'document.getElementById("sizeSelect").style.display = "none"';
$jsContent.= '</script>';
}
$smarty->assign('js', $jsContent);
Display the variable content as i said before, just after the select tag being targeted
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
{$js}
I think you should have a div in which select will work, use Div for hide and show, see below
Code for HTML:
<div id ="sizeSelect">
<select id="select_id" name="select_id">
<option>Small</option>
<option>Large</option>
</select>
</div>
and for php
if ($sizeSelect) {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}
This worked for me:
if ($sizeSelect != 1) {
echo '<script language="javascript" type="text/javascript">';
echo
'$( document ).ready(function() { $( "#sizeSelect" ).hide();});';
echo "</script>";
}else{
echo "<script language='javascript' type='text/javascript'>";
echo
"$( document ).ready(function() { $( '#sizeSelect' ).show();});";
echo "</script>";
this is jQuery by the way.

dont show javascript in if php

Hello for eveybody i have a trouble whit this
$aux=0;
if($resultado=='true'){
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
}
else{
if ($aux==0) {
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
$aux=1;
}
}
if ($resultado = true) its work fine but if ($resultado = false) dont show javascript code
Thanks in advance.
Get rid of the quotes around 'true' (and 'false'). This makes them strings and strings that are not empty always equate to true. This is because of type juggling in PHP.
if($resultado==true){
and
if($resultado==false){
there is no need to check wheather true and false as you just need to pass variable in if conditions and if condition just check the return true or false.Below is code
$aux=0;
if($resultado ){
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
}
else{
if (!$aux ) {
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
$aux=1;
}
}
$aux=0;
if($resultado=='true'){
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
}
else if ($aux==0) //-------->Brush up on your syntax for control structures
{
echo "<script type='text/javascript'>\n";
echo "alert('$resultado');\n";
echo "</script>";
$aux=1;
}

how to use javascript in php when there is a header?

if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "</script>";
header ("location: index.php");
}
it is a part of a php code. if i use // before header, javascript run..but when i don't use // , it goes to directly index.php. what is the solution?? OR is there is any way that i can call a javascript function for showing a message from this if condition??
In this scenario, Replace header with document.location.href So you can display an alertbox and when you click ok it redirects to your index.php page
if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "</script>";
echo "<script>document.location.href='index.php'</script>";
}
Can you try this,
<?php
if(isset($row["status"]) && trim($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "window.location.href='index.php';"; // instead php header you can use javascript location.hrfe
echo "</script>";
}
?>
try
if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "</script>";
}
else{
header ("location: index.php");
}
You can also try like this
if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "location: index.php";
echo "</script>";
}

Categories

Resources