So basically what im trying to do is pass a variable from a JS function to PHP variable.

Currently the onClick code is:

<?php include('onload.php'); ?>

<body>

<br /><br /><br /><br />
<center><font color="black" size=20><a style="text-decoration:none" href="#" onClick="showpopupshare()">

ShowpopupShare():

function showpopupshare()
{

	document.getElementById('mmpopup').style.display='block';
	document.getElementById('mmpopup1').style.display='block';
	window.setTimeout("StartTheTimer()",30000);
}

-- What im trying to do:

Index:

<?php include('onload.php'); ?>

<body>

<br /><br /><br /><br />
<center><font color="black" size=20><a style="text-decoration:none" href="#" onClick="showpopupshare(2)">

Notice the showpopupshare(2)

Then do this:

function showpopupshare(id)
{
	$phpmyvariable = id;
	document.getElementById('mmpopup').style.display='block';
	document.getElementById('mmpopup1').style.display='block';
	window.setTimeout("StartTheTimer()",30000);
}

I know that isn't going to work but how can i achieve the same effect? The page can't be reloaded.

Recommended Answers

All 9 Replies

Willing to Paypal who ever solves this issue $50 :)

Even though you have labelled showpopupshare as php, it is Javascript code so I would think that your onclick could go to your javascript code and never go back to PHP. Thus, it could all be one module. I think that it would look like:

onClick="javascript: showpopupshare('2');"

with showpopupshare as a javascript function in the head part of the same script.

If you need to go to PHP then the "normal way would be something like:

onClick="go('showpopupshare.php?id=2')"

but I don't think that is what you want to do in this case.

I'm not a js guy but from what I know, that is what you need to do. Maybe someone who is a js expert can confirm this.

Hi,

What exactly are you trying to accomplish? What is the significance of the variable in PHP? Does it affect the content that is loaded?

R.

as you can see: onload.php has alot more than just JS.

<?php session_start(); include('admin/config.php');

$profile = $_GET['profile'];

$ip = $_SERVER['REMOTE_ADDR'];

if(!isset($_SESSION['referer_id']) && mysql_num_rows(mysql_query("select * from il_referals where ip='$ip' and `date`=NOW()"))== 0 )

{

	$refer = $_SERVER["HTTP_REFERER"];

	if($refer == '')

	$refer = 'Direct';

	mysql_query("insert into il_referals set ip='$ip',`date`=NOW(),referer='$refer'");

	$_SESSION['referer_id'] = mysql_insert_id();

}	

if(mysql_num_rows(mysql_query("select * from il_referals where ip='$ip' and `date`=NOW()"))!= 0)

{

	$r = mysql_fetch_array(mysql_query("select * from il_referals where ip='$ip' and `date`=NOW()"));

	$_SESSION['referer_id'] = $r['fk_id'];

}?>

<link rel="stylesheet" href="css/all.css" type="text/css" media="all" />
<link rel="stylesheet" href="css/sIFR-screen.css" type="text/css" media="screen" />
<script src="css/sifr.js" type="text/javascript"></script>


<script>

function closeall()
{
	document.getElementById('mmpopup').style.display='none';
	document.getElementById('mmpopup1').style.display='none';
}

function showpopupshare()
{

	document.getElementById('mmpopup').style.display='block';
	document.getElementById('mmpopup1').style.display='block';
	window.setTimeout("StartTheTimer()",30000);
}

function chkrewmsg()
{
	var i = document.getElementById('istoshowrewardmsg').value;
	if(i==1)
	{
		document.getElementById('mmpopup1').style.display='none';
		document.getElementById('mmpopup2').style.display='block';
	}
	else
	{
		document.getElementById('mmpopup').style.display='none';
		document.getElementById('mmpopup1').style.display='none';	
	}
}

function StartTheTimer()
{
   	checkReferrals(); // check db stats
   
	if(parseInt(document.getElementById('current').innerHTML) == parseInt(document.getElementById('total').innerHTML) || parseInt(document.getElementById('current').innerHTML) > parseInt(document.getElementById('total').innerHTML))
	{
		chkrewmsg()
	}
	else
	{
		window.setTimeout("xmlhttpvdft_AL('check.php')",1000)
	}

	setTimeout("StartTheTimer()", 30000); // 30 seconds
}


function checkReferrals()
{
	document.getElementById('visitorsent').innerHTML = '<center>Checking Visitors...</center>';
	document.getElementById('visitorequired').innerHTML = '';
	setTimeout("checkReferralsMain()", 3000);
}

function checkReferralsMain()
{
	document.getElementById('visitorsent').innerHTML = '<?php getusers(); ?>';
	document.getElementById('visitorequired').innerHTML = 'Visitors Required: <?php getrequired(); ?>';
}

function newWindow(url)
{
  myRef = window.open(''+url,'mywin','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');
}

</script>

<body>

<?php include('admin/config.php');

$r = mysql_fetch_array(mysql_query("select * from il_pageparameters"));

function getusers()
{
	$ip = $_SERVER['REMOTE_ADDR'];
	$result = mysql_fetch_array(mysql_query("SELECT fk_id FROM il_referals WHERE ip='$ip'"));
	$id = $result['fk_id'];

	$users = mysql_num_rows(mysql_query("select * from il_totalreferals where fk_id=$id"));
	echo 'Visitors Sent: ' .$users;
}

function getrequired()
{
	$r = mysql_fetch_array(mysql_query("select * from il_pageparameters"));
	echo $r['totalvisitor_to_refer'];
}

function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
  //create the URL
  $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
  
  //get the url
  //could also use cURL here
  $response = file_get_contents($bitly);
  
  //parse depending on desired format
  if(strtolower($format) == 'json')
  {
    $json = @json_decode($response,true);
    return $json['results'][$url]['shortUrl'];
  }
  else //xml
  {
    $xml = simplexml_load_string($response);
    return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
  }
}

?>

</body>



<table width="100%" align="center" style="position:fixed; top:0px; left:0px; height:1000px; 

 margin:0px; background-image:url(admin/images/1.png); display:none;" id="mmpopup" cellpadding="0" cellspacing="0">

	<tr>

		<td width="40%"></td>

		<td align="center" valign="top">			

			<div  id="mmpopup1" style="margin:0 auto; display:none;position:relative;top:200px; background-image:url(<?php echo $r['background_image'];?>); width:539px; height:264px;">

Basically i want to be able to do:

$r = mysql_fetch_array(mysql_query("select * from il_pageparameters WHERE id = JS VARIABLE HERE"));

javascript is not a serverside its a client side. it cannot query a database.

where is il_pageparameters saved at? if its using mysql(i) like you've defined its not going to work with js variables, just put the field of your MySQL table your calling for. if your selecting it with a wildcard its going to call each one of them anyways, why would you need to id it?

I really don't understand what your trying to say..........

il_pageparameters is a table in my DB.

The table has multiple rows, ID=1, ID=2, ID=3, etc.

I want to be able to tell it which one to load.

The answer is no, you cant call javascript functions using php or call javascript varibles using php, vice versa. Does this address your issue? if im way off target let me know and clarify your question.

Well i know i can't do it like that. Hence why im paying for a solution....

Hi,

I too am still not clear as to what you're trying to achieve. To create some form of asynchronous interaction between client and server side, you need to use AJAX. This sort of task is far easier if you make use of a JavaScript library. My personal preference lies with jQuery.

Using jQuery, this could be accomplished with this simplified example:

$(function() {
    var id = 1;    // Your JS variable

    // The ajax request will be executed when an anchor tag with id 'example' is clicked
    $('a#example').click(function(event){
        event.preventDefault();

        $.ajax({
            type: 'post',
            url: 'lookup.php',
            data: 'id=' + id,
            success: function(data) {
                $('.result').html(data);    // This will load the result in an element with class 'result'
            }
        });
    });
});

Then in your PHP script you would access the id variable like any other POST variable:

<?php

$id = (isset($_POST['id']) ? (int)$_POST['id'] : 0;

$sql = sprintf("select * from il_pageparameters where id = %d", $id);
$result = mysql_fetch_array(mysql_query($sql));

// Then output the result however you want
var_dump($result);
exit();

There are lots of other events which can be used. Or if you prefer vanilla JS, this can be accomplished with that too.

R.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.