My script part in my php page is like this
<script type="text/javascript" >
alert('Out');
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
</script>
While the code below is what I get from the Chrome Developers tool (Resource>XHR) (which I'm using for debugging)
<!-- For javascripts -->
<script type="text/javascript" >
alert('Out');
if (1 == 1) {
alert('ting');
}
</script>
The alerts are not popping up.
What is wrong with my codes??
Update
I've my codes in a file add-line.php
<script type="text/javascript" >
// $(function() {
if (<?php echo $timer; ?> == 1) {
//alert('Line');
var timenow = <?php echo time(); ?>;
var dataPass = 'lineId=' + <?php echo $lineId; ?> + '&storyId=' + <?php echo $storyId; ?> + '&timenow=' + timenow;
$.ajax({
type: "POST",
url: "proc/updateDb.php",
data: dataPass,
cache: false,
success: function(){
// Show error if error is there
}
});
}
// });
</script>
Now in the updateDb.php I m processing some CRUD thing using the POST values and then determine $delFlag value. Then at the end of this file, I've my scripts as (same as the first one above)
<script type="text/javascript" >
alert('Out');
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
</script>
But this script is not being executed at all it seems.
As I notice you are using ajax, you that script would run if you add it to the DOM.
for example, if you use $.ajax()
$.ajax({
url: 'some/url/',
type: 'html',
method: 'get',
success: function(data){
$('head').append(data);
// data here would be the response from the server...
}
});
You tried this:
<!-- For javascripts -->
<script type="text/javascript" >
var number = parseInt("<?php echo $delFlag; ?>");
alert('Out');
if (number == 1) {
alert('ting');
}
</script>
You can do it without the quotes if you want.. but if you using some editor they whill markit as a error.
Try like that. Error message will prompt and try to fix it.
<script type="text/javascript">
alert('Out');
try
{
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
}
catch(err)
{
alert(err.description);
}
</script>
Related
Hi I have multiple Bootsyrap modals created dynamically with php , these modal opened itself if requirements meets, I want to open one after another closed , and problem is not sure all three modals will meet requirements , some time we have 2 , some time one so I can not use modal close event as well
// 1st modal
<?php if(count($getNewBadges) >0 ) { ?>
<script>
$(document).ready(function() {
$("#EarnBadgeModal").modal('show');
$.ajax({
type: "POST",
url: "<?php echo SITE_URL; ?>/engagement/award/ajax/award_ajax.php",
data: { action:'set_badge_popup' },
success: function(response){
$("#EarnBadgeModal").modal('hide');
}
});
});
</script>
<?php } ?>
// 2nd modal
<?php if(count($getNewContest) >0) { ?>
<script>
$(document).ready(function() {
$("#ContestUserModal").modal('show');
$('#ContestUserModal').css('z-index', 1040);
$.ajax({
type: "POST",
url: "<?php echo SITE_URL; ?>/engagement/leaderboard/ajax/leaderboard_ajax.php",
data: { action:'set_contest_read' },
success: function(response){
}
});
});
</script>
<?php } ?>
// 3rd modal
if(count($getPoints) >0)
{
?>
<script>
$(document).ready(function() {
$("#pointsUserModal<?php echo $mv; ?>").modal('show');
$('#pointsUserModal').css('z-index', 1049);
});
</script>
<?php
}
I have tried many things like below , but nothing worked
<style type="text/css">
.modal-backdrop + .modal-backdrop {
z-index: 1051;
}
.modal-backdrop + .modal-backdrop + .modal-backdrop {
z-index: 1051;
}
</style>
// another
<style type="text/css">
.modal-backdrop:nth-child(2n-1) {
opacity : 0;
}
</style>
Thanks Any help is appreciated
I did like this as #Cbroe advised and its working fine now , added a class on bootstrap close button .closeModal
<script type="text/javascript">
$( document ).ready(function() {
for (i = 0; i < 1; i++) {
$(modalArr[0]).modal('show');
modalArr.shift();
}
});
$(".closeModal").on('click', function()
{
for (i = 0; i < 1; i++) {
$(modalArr[0]).modal('show');
modalArr.shift();
}
});
console.log(modalArr);
</script>
I have written a PHP code having single function with an echo statement and I wish to call that function as a url in ajax code.
I tired doing this.
<html>
<head>
<title>Writing PHP Function</title>
<script>
$.ajax(
{
url : localhost/practice.php/f=myFirst();
type: "GET",
data: dataString,
success: function(result)
{
alert(result);
}
});
</script>
</head>
<body>
<?php
function myFirst()
{
echo 'The First ran successfully.';
}
?>
</body>
</html>
There are a lot of things wrong with what you have. Here is just a simple example that should work for you.
practice.php
<?php
if(isset($_GET['f']))
echo strip_tags($_GET['f']);
?>
index.php
<?php function myFirst() {
echo 'The First ran successfully.';
} ?><html>
<head>
<title>Writing PHP Function</title>
<!-- You need to add the jQuery library -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
// You should activate it on an event like click (unless you want it to autoload)
$("#content").click(function() {
$.ajax({
// Your url is funky, and cannot be terminated with a semicolon
// but rather a comma
url : 'localhost/practice.php?f=<?php myFirst(); ?>',
type: "GET",
// You can do an alert, but I just made it populate a div instead
success: function(result) {
$("#place-to-load").html(result);
}
});
});
</script>
</head>
<body>
<!-- Your php function will not work as you intend because one -->
<!-- is a server-side function and the other is a client-side script -->
<!-- The functions are not interchangeable. That being said, you can -->
<!-- populate a javascript with a php variable but it would need to be -->
<!-- echoed like <?php myFirst(); ?> -->
<div id="place-to-load"></div>
<div id="content">Load</div>
</body>
</html>
I would try to call the functions based on the params from the URL.
Ajax part,
$.ajax({url:'localhost/practice.php?f=myFirst',type:'GET'}).done(function(response){
alert(response); // or you can write to your document
});
and PHP file
<?php
if(isset($_GET) && $_GET['f'] == 'myFirst'){
myFirst();
}else{
die('No GET params received');
}
function myFirst(){
echo 'The First ran successfully.';
}
?>
In insert.php I have the following
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".like").click(function() {
var data = $(this).attr('data-id');
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
});
});
});
// ...
</script>
<!-- ... -->
<?
if (!is_bool($result) && !is_null($result)){
while($row = mysql_fetch_array($result))
{
?>
<? $id = $row['uniqueid'];?>
<? echo "<br>ID: " . $row['uniqueid'];?>
<? echo "<br>Name: " . $row['surname']; ?>
<? echo "<br><a href class=like id=buton data-id='$id'> Like (" . $row['likes'] . ") </a>"; ?>
<? echo "<br><br>"; ?>
<!-- ... -->
in data.php I have
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
alert("<? echo $_POST['name']; ?>");
</script>
I fixed a couple of things I was told were wrong but it still does't work. The alert box is blank. Please help. Thanks.
this is completely wrong... for your data.php this will not parse javascript the way you are expecting.. it will simply take in POST data and ECHO out a result.. dont try and use client-side javascript because it will not do anything
data.php: its php just simply do
<?php
echo $_POST['name'];
?>
in your js if you really want a popup to show then you have to wait for the results of the POST/RESPONSE and do it
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
alert(response);
});
remember the php script you call from a POST or ajax call will only process server side code.. PHP, perl, java, C... not client-side code like javascript
I have not been able to use JQuery/Ajax to load the content of my PHP file into a div tag.
Here is my page that is loading the file:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
function init() {
reloadChat();
setInterval (reloadChat, 5000);
}
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(){
$("#chatmenu").load("chat.php");
},
});
}
</script>
<body onLoad='init()'></body>
<div id='chatmenu'></div>
The PHP file I am loading (chat.php) is in the same folder, and is just an echo statement:
<?php echo "test"; ?>
To make sure it was not a problem with my Javascript functions, I added an alert under the success function, and it did alert me every 5 seconds, so I think it is something with the load statement.
Use .load() straight away, no need to make an Ajax request first:
function reloadChat() {
$("#chatmenu").load("chat.php");
}
Update:
I notice that in your example code, you close your body-tag before your div element.
<body onLoad='init()'></body> <!-- This ain't right -->
<div id='chatmenu'></div>
Try this instead:
<body onLoad='init()'>
<div id='chatmenu'></div>
</body>
Try this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
function init() {
reloadChat();
setInterval (reloadChat, 5000);
}
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(response){
$("#chatmenu").text(response);
},
});
}
</script>
<body onLoad='init()'></body>
<div id='chatmenu'>
</div>
Also, for the love of god, please use an up-to-date version of jQuery
It looks your first request from $.ajax will return 'test', and then you're using that as the URL for $("#chatmenu").load.
Try this:
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(data){
$("#chatmenu").append(data);
},
});
}
Or, if you want to replace the contents of #chatmenu the method posted by Christofer Eliasson where you just call $("#chatmenu").load("chat.php") in reloadChat will work.
Putting it all together:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="chatmenu"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function() {
setInterval (function() {
$("#chatmenu").load("chat.php");
}, 5000);
});
</script>
</body>
</html>
this is what i have in the head tags of my html page to load the facebox plugin:
<!-- This is for the facebox plugin to work -->
<link href="<?php echo base_url();?>styles/facebox/src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>styles/facebox/lib/jquery.js" type="text/javascript"> </script>
<script src="<?php echo base_url();?>styles/facebox/src/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : "<?php echo base_url();?>styles/facebox/src/loading.gif",
closeImage : "<?php echo base_url();?>styles/facebox/src/closelabel.png"
})
})
</script>
this is the code for my div
<?php if(isset($$j)) : foreach($$j as $row) : ?>
<div class="single-result" id="single-result">
<div class="name"><?php echo $row->Name; ?></div>
<div class="description"><?php echo $row->Description; ?></div>
</div>
<?php endforeach; ?>
<?php else : ?>
error here
<?php endif; ?>
what i am wanting, is some ajax code that calls a function when the user clicks on the div id="single-result" so that it makes an ajax call to a php controller that gets the data from the database and then loads the view into a facebox modal window.
The part i am unsure about is how to make it load into a facebox modal window, l know how to use ajax calls to make it send data to a file and then load a view into a certain div on the page, l just want it to load it into the modal window instead of a certain div on the page.
Is this possible at all? (also i am using codeigniter for the php framework)
here is my ajax code which would load the ajax request results into the content-right div when the user clicks on div.
<script type="text/javascript">
$("#single-result").click(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
$('#content-right').html(msg);
}
});
return false;
});
</script>
<!-- END -->
how do i modify this code above to make it work with going into a facebox modal window? I know facebox provides this code below, but i have no idea what to do with it?
jQuery.facebox(function() {
jQuery.get('code.js', function(data) {
jQuery.facebox('<textarea>' + data + '</textarea>')
})
})
It's not really clear what you're after...
If what you want is to have the data returned from the ajax POST to be populated into a facebox, simply call facebox on the success callback of the ajax post:
$("#single-result").click(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
jQuery.facebox(msg);
})
});
alternatively, it looks like if you want the facebox loading graphics while the post is processed, you can wrap the ajax POST in facebox function:
$("#single-result").click(function() {
jQuery.facebox(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
jQuery.facebox(msg);
})
});
})
})