I have one problem. I have a popup and after a user creates a room in the popup, I want to close that popup and then redirect the opener window to a new url. I have the function and everything, except for I do not know how to call the function. (Below I have written where I want the function to load).
Thanks
<?php session_start(); ?>
<script type="text/javascript">
function CloseAndRefresh()
{
opener.location.href = "<?php echo $website_url; ?>/livechat.php?idim=<?php echo $perdorusi2; ?>&room=<?php echo $emri; ?>";
opener.focus();
self.close();
}
</script>
<?php
include_once('db.php');
$perdoruesi = $_GET['perdoruesi'];
if( strlen($_SESSION['id']) > '0' ) { $perdorusi2 = $_SESSION['id']; } else { $perdorusi2 = $perdoruesi; }
function makeRandomString($max=8) {
$i = 0;
$possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$keys_length = strlen($possible_keys);
$str = "";
while($i<$max) {
$rand = mt_rand(1,$keys_length-1);
$str.= $possible_keys[$rand];
$i++;
}
return $str;
}
$emri = makeRandomString();
$hidhnedb = mysql_query("INSERT INTO dbname(`row1`, `row2`) VALUES(NULL, '$emri')");
if($hidhnedb) {
$max=count($_SESSION['mysession']);
$i = $max + 1;
$_SESSION['mysession'][$i][$emri] = $emri;
?>
***Here is the the place where I want to call the javascript function but I do not know how to do that.***
<?php
} else { echo "Error!"; }
?>
What you named "popup" is not really clear: is it another browser window ? An window.confirm or window.alert dialog ? a popup like in jQueryUI or Twitter Bootstrap ?
Let's assume it's a browser window.
You have two possibilities:
1 - Make a single button to close your popup:
<button onClick="CloseAndRefresh()">Close</button>
2 - Trigger it automatically after N seconds:
<script type="application/javascript">
var n = 5;
setTimeout(CloseAndRefresh, 1000*n);
</script>
Try
<script type="text/javascript">
CloseAndRefresh();
</script>
Although a PHP redirect would probably fit your purpose better.
Here is an article on PHP redirects:
http://php.about.com/od/learnphp/ht/phpredirection.htm
You can't invoke Javascript functions from PHP as PHP is running on the server, whereas Javascript is downloaded and run on the client, long after the PHP code has executed. You can however print a <script> tag where you, in this case, invoke the Javascript function you want:
<?php
echo '<script>(function () { CloseAndRefresh(); } ());</script>';
After searching for a long time I've decided to just ask it here, since the things I found either don't work(or I can't get them to work ;)) - or require me to change things on my server, which I would like to avoid.
I want below function to show the output after each result, so the function doesn't have to be loaded first, which takes a long time. My idea, as you can see below, was to try this with jQuery(1.8.2).
It doesn't work and I simply can't get it to work. Is there a better way to do this? Did I make a mistake somewhere causing it not to work?
If you need additional information, please ask.
<?php
print_r($_POST);
if(isset($_POST['p']) && isset($_POST['domain']) && isset($_POST['option']))
{ error_reporting(0);
define('INCLUDE_CHECK',true);
require('admin/API/class_api.php');
require('admin/functions/core.inc.php');
$dom = explode('.',$_POST['domain']);
$dom = $dom[0];
$ext = array('nl','be','eu','net','com','org','biz','info','tk','ws','gr','me','cc','in','gs','name','ch','co','tv','ru','bz','li','lu','pl','se','vg','cx','tl','im','sg','ms','sh','io','mu','fm','am','xxx','ag','sc','nf','md');
if ($_POST['loop']==40)
{ print 'Laatste loop dus .. STOP : .'.$ext[41]; }
else
{ print 'Loop: '.$_POST['loop'].' - zoek op : .'.$ext[$_POST['loop']];
?>
<script>
$.post('test.php', { p:'full', domain:dom, option:opt, loop:'<?php echo $_POST['loop']+1; ?>'},
function(data){
$('#domresults2').css('display','block').html(data);
$('#domloading').css('display','none');
});
</script>
<?php
}
}
?>
//Start JS
<script>
$('#domsubmit.complete').click( function() {
var dom = escape($('#domsearch').val());
var opt = escape($('#option').val());
$('#domresults2').css('display','none').html('');
$('#domloading').css('display','inline');
$.post('test.php', { p:'full', domain:dom, option:opt, loop:0},
function(data){
$('#domloading').css('display','none');
$('#domresults2').css('display','block').html(data);
});
return false;
});
</script>
//end JS
///////////////////////////
update : solved!
so, what went wrong? -> the php function in core.inc.php referred to a incompatible with jQuery 1.8.2 lib ('qTip2'). Thanks for the support!
<?php
print_r($_POST);
if (isset($_POST['p']) && isset($_POST['domain']) && isset($_POST['option'])) {
error_reporting(0);
define('INCLUDE_CHECK', true);
require('admin/API/class_api.php');
require('admin/functions/core.inc.php');
$dom = explode('.', $_POST['domain']);
$dom = $dom[0];
$ext = array('nl','be','eu','net','com','org','biz','info','tk','ws','gr','me','cc','in','gs','name','ch','co','tv','ru','bz','li','lu','pl','se','vg','cx','tl','im','sg','ms','sh','io','mu','fm','am','xxx','ag','sc','nf','md');
$loop = $_POST['loop'];
DomCheck($_POST['domain'],$ext[$_POST['loop']]);
if ($loop != count($ext)) {
$loop++;?>
<script type="text/javascript">
var opt = '<?php echo $_POST['option']; ?>';
var dom = '<?php echo $_POST['domain']; ?>';
$.post('test.php', { p:'full', domain:dom, option:opt, loop:'<?php echo $loop; ?>'},
function(data){
$('#domresults2').css('display','block').append(data);
<?php if ($loop < count($ext)) {
echo "$('#domloading').css('display','none');";
} ?>
});
</script>
<?php
}
}
?>
One thing you need to understand is that PHP executes on the server and JavaScript (jQuery) executes on the client.
This means that by the time jQuery starts executing, PHP is done.
However, I don't see a loop anywhere in your code. I think this is more of what you wanted. You're not doing a "PHP loop using jQuery," you're outputting jQuery code in a PHP loop:
<?php
if (isset($_POST['p']) && isset($_POST['domain']) && isset($_POST['option'])) {
error_reporting(0);
define('INCLUDE_CHECK', true);
require('admin/API/class_api.php');
require('admin/functions/core.inc.php');
$dom = explode('.', $_POST['domain']);
$dom = $dom[0];
$ext = array('nl','be','eu','net','com','org','biz','info','tk','ws','gr','me','cc','in','gs','name','ch','co','tv','ru','bz','li','lu','pl','se','vg','cx','tl','im','sg','ms','sh','io','mu','fm','am','xxx','ag','sc','nf','md');
foreach ($_POST['loop'] as $loop) {
if ($loop == 40) {
print "Laatste loop dus .. STOP : .{$ext[41]}";
} else {
print "Loop: {$loop} - zoek op : .{$ext[$loop]}";
?> <script>
$.post('test.php', { p:'full', domain:dom, option:opt, loop:'<?php echo $loop; ?>'},
function(data){
$('#domresults2').css('display','block').html(data);
$('#domloading').css('display','none');
});
</script>
<?php }
}
}
?>
I am building a simple antibot for my email form.
This is a code which makes a problem:
<?php
session_start();
$a = rand(1, 10);
$b = rand(1, 10);
$antibot = $a + $b;
$_SESSION["antibot"] = $antibot;
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#sendEmail").click(function(){
var antibot = $("#antibot").val();
$.post(
"test.php",
{antibot: antibot},
function(data){
alert(data.info);
},
"json"
);
);
);
);
</script>
</head>
<body>
<table>
<tr><td>AntiBot: <?php echo $a . ' + ' . $b;?> = </td><td><input type='text' id='antibot' /></td></tr>
<tr><td colspan='2'><input type='button' id='sendEmail' value='Send'/></td></tr>
</table>
</body>
</html>
And my test.php
<?php
session_start();
$antibot = $_POST["antibot"];
$data = array();
if(isset($_SESSION["antibot"])){
if($_SESSION["antibot"] == $antibot){
$data["info"] = "Session is isset and answer is right!";
}
else{
$data["info"] = "Session is isset but answer is NOT right!";
}
}
else{
$data["info"] = "Session is NOT isset!";
}
echo json_encode($data);
?>
I constantly get this info: "Session is isset but answer is NOT right!"
If you can see $_SESSION["antibot"] in test.php is setted but value is "" no matter what I type in input field #antibot!
I do not understand why this is happening, please can someone tell me where the problem is and how can I fix it?
I tested this code here:
http://onlinephpfunctions.com/stackoverflow/11981309/
And it seems completely valid.
I had to make some modifications to your javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#sendEmail").click(function(){
var antibot = $("#antibot").val();
$.post(
"test.php",
{antibot: antibot},
function(data){
alert(data.info);
},
"json"
)
})
})
</script>
After that it was working fine. It may just be some problems with cookies in your browser, or an error in your PHP config so sessions wont be stored right. Please check this, your code works OK, as you can see in the demo.
jQuery:
$(document).ready(function(){
$(".reload").click(function() {
$("div#update").fadeOut("fast")
.load("home.php div#update").fadeIn("fast")
});
});
PHP:
function statusUpdate() {
$service_query = mysql_query("SELECT * FROM service ORDER BY status");
$service_num = mysql_num_rows($service_query);
for ($x=1;$x<=$service_num;$x++) {
$service_row = mysql_fetch_row($service_query);
$second_query = mysql_query("SELECT * FROM service WHERE sid='$service_row[0]'");
$row = mysql_fetch_row($second_query);
$socket = #fsockopen($row[3], $row[4], $errnum, $errstr, 0.01);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online'; }
mysql_query("UPDATE service SET status='$status' WHERE sid='$row[0]'")
or die(mysql_error());
?>
<ul><li style="min-width:190px;"><?php echo $row[1]; ?></li>
<li style="min-width: 190px;" title="DNS: <?php echo $row[2]; ?>">
<?php echo $row[3] . ':' . $row[4]; ?></li>
<li class="<?php echo $status; ?>" style="min-width:80px;"><div id="update">
<?php echo $status; ?></div></li></ul>
<?php
}
}
?>
<?php statusUpdate(); ?>
I have a button which I press (refresh) and that will then refresh the #update id to hopefully fadeOut all the results, and then fade in the new results... issue is it fades them out okay, but when it brings them back, it's just div on div and div and looks really messy - does not do what it's meant to do (would have to upload a picture to give further information).
In the short, what I want to happen is when you hit the update, they will all fade and then fade in with updated values from the php... I made the php/mysql into a function so then I could call it when i hit that refresh button, thinking that would work, but I don't know how to do that...
Thank-you in advance,
Phillip.
Javascript
$(document).ready(function(){
$(".reload").click(function() {
$("div#update").fadeOut("fast");
$.ajax({
url:'home.php',
data:{type:'getStatus'},
type;'post',
success:function(data){
$('div#update').html(data).fadeIn('fast');
}
});
});
});
php page format
<?php
$type= $_POST['type'];
if($type=="getStatus")
{
//get statuses from data base and return only formatted statuses in html
}
else
{
//your page codes here
//like tags <html>,<body> etc, all regular tags
//<script> tags etc
}
?>
.load("home.php div#update").fadeIn("fast")
That's wrong. You need to use,
$('div#update').load('home.php', function(data) {
$('div#update').html(data).fadeIn("fast");
});
Make sure your PHP file works properly by calling it directly and confirming that it returns the results properly.
Reference : http://api.jquery.com/load
Try this
var $data = $('div#update');
$data.fadeOut('slow', function() {
$data.load('home.php div#update', function() {
$data.fadeIn('slow');
});
});
Just for the reference, it will be better to add an additional page in the same directory (eg: phpcode.php) and then put your php code also in there! then try this:
var $data = $('div#update');
$data.fadeOut('slow', function() {
$data.load('phpcode.php div#update', function() {
$data.fadeIn('slow');
});
});