Autoclose alert doesn't alert - javascript

I cannot se why my autoclose doesn't work.
I use php to echo out javascript. I do echo both script. First the "not working" and then the "working". Only the "working" script shows up!
What's wrong?
Not working
echo "<div class='alert-message'></div>";
echo "<script type='text/javascript'>";
echo "$('.alert-message').alert();";
echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";
Works
//Echo succes
echo "<script type='text/javascript'>";
echo "alert('Välkommen');";
echo 'window.location = "page.php"';
echo "</script>";
die();

.alert-message div is not yet loaded in DOM when you are using it. Try to echo it before script:
echo "<div class='alert-message'></div>";
echo "<script type='text/javascript'>";
echo "$('.alert-message').alert();";
echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";

Related

Modal doesn't open when php variables value is large

I am using an edit button inside while loop for each row of table to pass a row values into a modal form.
<td><a class="custom-links" onclick='EditModal("<?php echo $data['id']; ?>","<?php echo $data['name']; ?>","<?php echo $data['price']; ?>","<?php echo $data['description']; ?>","<?php echo $data['type']; ?>","<?php echo $data['cooking_instructions']; ?>","<?php echo $data['ingredients']; ?>","<?php echo $data['allergen_warnings']; ?>","<?php echo $data['storage_instructions']; ?>","<?php echo $data['case_size']; ?>","<?php echo 'uploaded_images/'.$data['image']; ?>")'>
<button type="button" id="<?php echo $data['id']; ?>" class='btn btn-primary glyphicon glyphicon-edit'></button></a></td>
Then i set values like
function EditModal(id,name,price,description,type,cooking_instructions,ingredients,allergen_warnings,storage_instructions,case_size,image){
document.getElementById("update_id").value = id;
document.getElementById("update_name").value = name;
document.getElementById("update_price").value = price;
document.getElementById("update_description").value = description;
document.getElementById("update_type").value = type;
document.getElementById("update_cooking_instructions").value = cooking_instructions;
document.getElementById("update_ingredients").value = ingredients;
document.getElementById("update_allergen_warnings").value = allergen_warnings;
document.getElementById("update_storage_instructions").value = storage_instructions;
document.getElementById("update_case_size").value = case_size;
document.getElementById("update_image_to_upload").src = image;
$('#update_menu_modal').modal('show');}
update_cooking_instructions, update_ingredients, update_allergen_warnings are textareas.
The problem is that modal gets opened when $data['cooking_instructions'], $data['ingredients'], $data['allergen_warnings'] contain less words or I remove them from EditModal function and when their values are large modal does not open.
Can anyone point out my mistake? What am I doing wrong? Modal is opening when I remove these three variables from EditModal
Give this a try, though note that you'll still have a problem with single quotes:
<td><a class="custom-links" onclick='EditModal(<?php echo json_encode($data['id']); ?>,<?php echo json_encode($data['name']); ?>,<?php echo json_encode($data['price']); ?>,<?php echo json_encode($data['description']); ?>,<?php echo json_encode($data['type']); ?>,<?php echo json_encode($data['cooking_instructions']); ?>,<?php echo json_encode($data['ingredients']); ?>,<?php echo json_encode($data['allergen_warnings']); ?>,<?php echo json_encode($data['storage_instructions']); ?>,<?php echo json_encode($data['case_size']); ?>,<?php echo json_encode('uploaded_images/'.$data['image']); ?>)'>
<button type="button" id=<?php echo json_encode($data['id']); ?> class='btn btn-primary glyphicon glyphicon-edit'></button></a></td>
I've removed the double quotes and instead used json_encode to emit a value. (I'm making the assumption that each value is a string on the PHP side.)
My guess as to the issue (though I'm stumped as to why it doesn't cause a console error) is that your "large" values have quotes or newlines in them. This sort of thing:
EditModal("<?php echo foo; ?>");
will turn into this if you have a newline:
EditModal("first line
second line");
which is not valid JavaScript. Similarly, a double quote would do this:
EditModal("Here is a quote --> " <-- see?");
which is also not valid JavaScript.
Using this instead:
EditModal(<?php echo json_encode(foo); ?>);
will turn those examples into these:
EditModal("first line\nsecond line");
and
EditModal("Here is a quote --> \" <-- see?");
both of which are fine.

$_GET not working on my php file [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a url link that has value of the id and process when clicked.
code:
<?php
<a href='settoActive.php?id=".$row['id']."&process=actives' style='font-size:15px;' name='active' value=".$row['id']." class='btn btn-info' />ACTIVE</a>
?>
<a href='settoActive.php?id=".$row['id']."&process=inactive' style='font-size:15px;' name='active' value=".$row['id']." class='btn btn-info' />inactive</a>
and this is where the page will be redirected to:
<?php
$value = $_GET['process'];
echo "<script> alert(".$value.");</script> ";
if($_GET['proc']="actives"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Active' where id=".$id." ") or mysqli0;
echo "<script>alert('Activessss!'); </script>";
//location.replace('addmodel.php')
}
if($_GET['process']="inactive"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Inactive' where id=".$id." ") or mysqli0;
echo "<script>alert('Inactive!'); </script>";
}
// location.replace('addmodel.php')
?>
The problem is the 2 if condition trigger and why it is triggering at the same time?
There are 3 mistakes noticed... 2 places '==' operator and 1 place GET variable name 'process'.
$value = $_GET['process'];
echo "<script> alert(".$value.");</script> ";
if($_GET['process']=="actives"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Active' where id=".$id." ") or mysqli0;
echo "<script>alert('Activessss!'); </script>";
//location.replace('addmodel.php')
}
if($_GET['process']=="inactive"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Inactive' where id=".$id." ") or mysqli0;
echo "<script>alert('Inactive!'); </script>";
}
// location.replace('addmodel.php')
?>
Problem in the html code, try following code
<a href='settoActive.php?id=<?php echo $row['id']; ?>&process=actives' style='font-size:15px;' name='active' value="<?php echo $row['id']; ?>" class='btn btn-info' />ACTIVE</a>
<a href='settoActive.php?id=<?php echo $row['id']; ?>&process=inactive' style='font-size:15px;' name='active' value="<?php echo $row['id']; ?>" class='btn btn-info' />inactive</a>

AJAX code to save id value in database

I want to pass this value doctoridclickedRightNow into t_appointmentdetails table. I have made few changes to previous code using AJAX and JS as suggested on How to Pass variable from JavaScript to PHP Page using XMLHTTP . Appointment details are getting inserted into DB and I also want the doctorid of the corresponding doctor to be saved. So with the below changes I am able see the correct values being displayed when I use inspect element in mozilla. Kindly suggest me how to pass this to DB. Thanks in advance.
$sql = "SELECT doctorid, FIRST_NAME,LAST_NAME, Address, Qualification, Specialization, Consultation_Fee, Experience,image_path
from t_doctorprofile";
$results = mysqli_query($db,$sql)
or die('MySQL Error');
echo '<h3 style="color:#674172;margin- left:120px;float:center;display:inline-block"><strong>Available Dentists</strong></h3>';
while($result = mysqli_fetch_assoc($results)){
echo '<div class= "fetch" style="margin-left:120px">';
echo "<img src = 'uploads/".$result['image_path']."' width='150' height='150'>";
echo "<div id='info'>";
echo "<p><strong>".$result['FIRST_NAME']." ".$result['LAST_NAME']."</strong></p></div>";
echo "<p>".$result['Qualification']."</p>";
echo "<p>".$result['Specialization']."</p>";
echo "<p>".$result['Address']."</p>";
echo "<p>"."<b>Consultation Fee-</b>"." ".$result['Consultation_Fee']."</p>";
echo "<p>"."<b>Experience-</b>"." ".$result['Experience']."years"." </p>";
echo "<button class='btn btn-success btn-lg' id='$result[doctorid]' data-toggle='modal' data-target='#myModal' onclick='AssignDoctorIdClicked(id)'>
Appointment</button>";
echo "<script>var doctoridclickedRightNow;</script>";
echo "</div>";
echo '<form action="appointment_insert.php" method="POST">';
echo "<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>";
echo "<div class='modal-dialog'>";
echo "<div class='modal-content' style='background-color:#F1A9A0'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>";
echo "<h4 class='modal-title' id='myModalLabel' style='text-align:center; color:#fff'>
<strong>Book Appointment</strong></h4><br>";
echo "</div><br><br>";
echo "<div class='modal-body'>";
script for AssignDoctorIdClicked function:
<script>
function AssignDoctorIdClicked(id) {
console.log(id);
var doctoridclickedRightNow = id;
console.log(doctoridclickedRightNow);
</script>
appointment_insert.php page
if (isset($_POST["submit"])) {
$login_user = $_SESSION['username'];
$datetime = $_POST['datetime'];
$info = $_POST['info'];
$recieved_1 = $_GET['FIRST_NAME'];
$query = mysqli_query($db, "INSERT INTO t_appointmentdetails (patient_name,datetime,info) VALUES ('$login_user','$datetime','$info');");
$insertedDataBoolean = true;
if($insertedDataBoolean){
echo "Name: " . $recieved_1 . " booked successfully.";
}
else{
echo "Data-insertion error.";
}
}

JavaScript on change function working not well

I have a JavaScript code which works with HTML select tag.
$(function() {
$('#to_name').on('change',function() {
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>") {
$('#to_designation').val('<?php echo $ceo_chunks[3];?>');
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>') ;
if( $(this).val()=="<?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?>") {
$('#to_designation').val('<?php echo $liaison_one_chunks[3]?>')
$('#dear_sir').val('<?php echo $liaison_one_chunks[0]?> <?php echo $liaison_one_last_name;?>')
$("#liaison_one").show()
$("#dear_liaison_one").show()
} else {
$("#ceo").hide()
$("#liaison_two").hide()
$("#dear_ceo").hide()
$("#dear_liaison_two").hide()
}
if( $(this).val()=="<?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?>") {
$('#to_designation').val('<?php echo $liaison_two_chunks[3]?>')
$('#dear_sir').val('<?php echo $liaison_two_chunks[0]?> <?php echo $liaison_two_last_name;?>')
$("#liaison_two").show()
$("#dear_liaison_two").show()
} else {
$("#ceo").hide()
$("#liaison_one").hide()
$("#dear_ceo").hide()
$("#dear_liaison_one").hide()
}
};
});
});
and here is html code
<select name="to_name" id="to_name">
<option value="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"><?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?></option>
<option value="<?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?>"><?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?></option>
<option value="<?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?>"><?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?></option>
</select>
<br>
<select name="to_designation" id="to_designation">
<option value="<?php echo $ceo_chunks[3]?>" id="ceo"><?php echo $ceo_chunks[3]?></option>
<option value="<?php echo $liaison_one_chunks[3]?>" id="liaison_one"><?php echo $liaison_one_chunks[3]?></option>
<option value="<?php echo $liaison_two_chunks[3]?>"id="liaison_two"><?php echo $liaison_two_chunks[3]?></option>
</select>
<div id="dear_ceo" style="margin-top:10px; width:auto">
<input type="text" name="dear_sir" id="dear_ceo" value="Dear <?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;
?>"/>
</div>
<div id="dear_liaison_one" style="margin-top:10px; width:auto; display:none">
<input type="text" name="dear_sir" id="dear_liaison_one" value="Dear <?php echo $liaison_one_chunks[0]?> <?php echo $liaison_one_last_name;?>"/>
</div>
<div id="dear_liaison_two" style="margin-top:10px; width:auto; display:none">
<input type="text" name="dear_sir" id="dear_liaison_two" value="Dear <?php echo $liaison_two_chunks[0]?> <?php echo $liaison_two_last_name;?>"/>
</div>
My function works very well for select tag but it's not working with input type text.
How can I fix that?
There is a closing bracket clearly missing for your first if statement.
Please close the bracket. There is no logical error in your code.
$(function() {
$('#to_name').on('change',function(){
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"){
$('#to_designation').val('<?php echo $ceo_chunks[3];?>')
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>')
/*$("#ceo").show()
$("#dear_ceo").show()*/
//} ---uncomment this ---
also include a terminating semi-colon everytime you are calling a function. For example
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"){
$('#to_designation').val('<?php echo $ceo_chunks[3];?>');
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>');
}
I suggest you use semicolons at the end of line (when appropriate). I know they are not mandatory but consider them as a good practice.
You also have a semicolon at the end of if-clause at the end. It's probably not braking anything but just in case...
Try replacing spaces with underscores (_).
For example:
<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>
would become
<?php echo $ceo_chunks[0];?>_<?php echo $ceo_chunks[1];?>_<?php echo $ceo_chunks[2]?>
And check if you can alert the value of the changed input correctly.

pass dynamic id in jquery for select option

How to pass the id (generated dynamically in while loop ) to jquery.
echo '<div><td><select name=categoryname id="name-first'.$i.'" >';
echo '</select></td>';
$(function(){
$('#name-first+<?php echo $i; ?>').on('change',function(){
var selIndex= $("#name-second+<?php echo $i; ?> option:selected").index();
$("#name-second+<?php echo $i; ?> option").eq(selIndex).prop('selected', true);
});
});
You have an extra + in there which is not there when you generate the id:
$('#name-first+<?php echo $i; ?>').on('change',function(){
should be
$('#name-first<?php echo $i; ?>').on('change',function(){

Categories