How to call bootstrap modal in php - javascript

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>";

Related

How to show alert box after successful or not data deletion in mssql

I want to show JavaScript alert after successful or not data deletion in MSSQL. How to do this? I have written this code but it shows only the message=success part alert everytime, even when the deletion dont work becasue of errors like "conflict with reference(foreign_key)" So when i click on this link.
echo "<a class='activater' href='ma_QualiOverviewloeschen.php?TestaufstellungID=".$row['TestaufstellungID'] ."&QualiID=".$row['QualiID'] ."' title='Qualitest löschen' data-toggle='tooltip' onclick='confirm_delete()'> <span class='glyphicon glyphicon-trash'></span></a>";
It calls the following php Page, which handle the SQL Part:
$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");
$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");
$sth = $connection->prepare($QualiDelete);
$sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));
if($sth)
{
header("location: ma_QualiOverview.php?message=success");
}
else
{
echo sqlsrv_errors();
header("location: ma_QualiOverview.php?message=failed");
}
$connection = null;
Back to the main page where the link is clicked the following ifelseconsider on messageshould Show me the right alert.
<?php
if($_GET['message']=='success'){
echo '<script language="javascript">';
echo 'alert("Erfolgreich gelöscht.");';
echo '</script>';
} elseif($_GET['message']=='failed'){
echo '<script language="javascript">';
echo 'alert("Nicht gelöscht, da Quali "ongoing" ist.");';
echo '</script>';
}
?>
What do i miss?
$sth will never be falsy, you have to check the return value of $sth->execute
Also, you should echo the errors after sending out the header.
Since $sth is always defined, you always get the success result
See the modified code here
$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");
$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");
$sth = $connection->prepare($QualiDelete);//Check the value returned instead of $sth
$result = $sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));
if($result )
{
header("location: ma_QualiOverview.php?message=success");
}
else
{
header("location: ma_QualiOverview.php?message=failed");
echo sqlsrv_errors();//Echo must be after header
}
$connection = null;

Alert() javascript function not working in php

I know this is frequently asked question however I have tried using :
script language='javascript'
placed header in else after alert
script type='text/javascript'
Still I don't get alert box, while else parts executes perfectly.
Here's my code:
<?php
/* header('Content-Type: application/json');
$response = array(); */
if (isset($_GET['sid'])){
$con = mysqli_connect("localhost", "root", "", "kaemiphk_greivance");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$checkdata=mysqli_query($con,"SELECT * FROM officer_master WHERE pf_no = '".$_GET['sid']."'");
$query_data=mysqli_num_rows($checkdata);
if ($query_data == 0) {
//echo alert "welcome";
echo '<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js">';
echo "alert('PF No. Does not exist. Please Contact Admin!!!');";
echo '</script>';
}
else{
header('Content-Type: application/json');
$response = array();
$select="SELECT m.officer_name,m.email,m.department,m.mobile_no,m.designation,n.quarter_no,n.address,n.colony,n.blueprint_quarter,n.type_of_quarter, n.area FROM officer_master m, quarter_master n WHERE n.pf_no='".$_GET['sid']."' AND m.pf_no = n.pf_no";
$result = mysqli_query($con, $select); //mysql_query($qry);
while ($row = mysqli_fetch_assoc($result)) {
array_push($response, $row);
}
}
echo json_encode($response);
}
?>
What am I missing here.
Thanks
You have your js files mixed up.
Include jquery and then your script, inside separate tags:
echo '<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js" ></script>';
echo '<script type="text/javascript">';
echo "alert('PF No. Does not exist. Please Contact Admin!!!');";
echo '</script>';
By the way, you do NOT need jquery for a simple alert, as it is plain javascript. Try to avoid including external library if not needed, you will end up with a bloated code.
And printing js with php it's a bit of a hack. Why not just print it into your html or js file?
Javascript inside a script tag that has an src attribute does not get executed, you have to create a second script tag after the jquery one.
<?php
/* header('Content-Type: application/json');
$response = array(); */
if (isset($_GET['sid'])){
$con = mysqli_connect("localhost", "root", "", "kaemiphk_greivance");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$checkdata=mysqli_query($con,"SELECT * FROM officer_master WHERE pf_no = '".$_GET['sid']."'");
$query_data = mysqli_num_rows($checkdata);
if ($query_data == 0) {
//echo alert "welcome";
echo '<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js">';
echo '</script>';
echo "<script>alert('PF No. Does not exist. Please Contact Admin!!!');</script>";
} else {
header('Content-Type: application/json');
$response = array();
$select="SELECT m.officer_name,m.email,m.department,m.mobile_no,m.designation,n.quarter_no,n.address,n.colony,n.blueprint_quarter,n.type_of_quarter, n.area FROM officer_master m, quarter_master n WHERE n.pf_no='".$_GET['sid']."' AND m.pf_no = n.pf_no";
$result = mysqli_query($con, $select); //mysql_query($qry);
while ($row = mysqli_fetch_assoc($result)) {
array_push($response, $row);
}
}
echo json_encode($response);
}
}
?>

Form values are not inserting into my database

My form values are printing on this page but they are not inserting into my
database. I don't know what is wrong? If all the values are printing
that means values are reaching on this particular page but not going into
database.Even the columns in the database are sorted, but it still doesn't
work.
<?php
include("connection.php");
if(isset($_POST['continue']))
{
echo $eta_type = $_POST['eta_type'];
echo $firstname = $_POST['firstname'];
echo $lastname = $_POST['lastname'];
echo $title1=$_POST['title1'];
echo $dob=$_POST['dob'];
echo $gender=$_POST['gender'];
echo $nationality=$_POST['nationality'];
echo $cob=$_POST['cob'];
echo $passportnumber=$_POST['passportnumber'];
echo $pid=$_POST['pid'];
echo $ped=$_POST['ped'];
echo $residence=$_POST['residence'];
echo $possesseta=$_POST['possesseta'];
echo $multipleentry=$_POST['multipleentry'];
echo $arrivaldate=$_POST['arrivaldate'];
echo $purpose=$_POST['purpose'];
echo $final_dest=$_POST['final_dest'];
echo $stay_days=$_POST['stay_days'];
echo $add1=$_POST['add1'];
echo $add2=$_POST['add2'];
echo $city=$_POST['city'];
echo $state=$_POST['state'];
echo $pscode=$_POST['pscode'];
echo $country=$_POST['country'];
echo $addlanka=$_POST['addlanka'];
echo $email=$_POST['email'];
echo $alternate_email=$_POST['alternate_email'];
echo $phone=$_POST['phone'];
echo $mobile=$_POST['mobile'];
echo $fax=$_POST['fax'];
$date1 = date('Y-m-d', strtotime($dob));
$date2 = date('Y-m-d', strtotime($pid));
$date3 = date('Y-m-d', strtotime($ped));
$date4 = date('Y-m-d', strtotime($arrivaldate));
$address=$add1.$add2;
$sql= "INSERT INTO
user(applicationtype,
surname,givenname,
title,dob,gender,nationality,
cob,passportnumber,pid,ped,
residenceinsrilanka,processeta,
multipleentryofsrilanka,intentedarrivaldate,
purposeofvisit,finaldestination,staydays,address,
city,state,postalcode,country,addinsrilanka,email,
alternateemail,telephonenos,mobilenos,faxnos)
VALUES('$eta_type','$lastname','$firstname','$title1',
'$date1','$gender','$nationality','$cob',
'$passportnumber','$date2','$date3','$residence',
'$possesseta','$multipleentry','$date4','$purpose',
'$final_dest','$stay_days','$address','$city',
'$state','$pscode','$country','$addlanka','$email',
'$alternate_email','$phone','$mobile','$fax')";
$query = mysqli_query($conn, $sql);
if($query)
{
header("Location: continue-to-pay.php?pno=$passportnumber");
}
else
{
echo "<h4 style='color:red'>Failed.</h4>";
}
}
?>
Incredibly dangerous code...
Read some about query injection....
Any value passed this way can break your query.
You need to escape your values with mysqi_escape_string()
http://php.net/manual/fr/mysqli.real-escape-string.php

cant echo javascript alert from php

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

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.

Categories

Resources