Store Community Support Documentation
Hi all,

I'm trying to send a javascript variable into the PHP code.
I'm able to do it just in a way that it's not correct for my goal.

To be more precise:

I've created a new product based on the event module.
The two following files are present:

/module/my_events/include/component/block/my_events.class.php

Quote:
echo "today : ".$_GET['today']."";
$todayJS = $_GET["today"];



/module/my_events/template/default/block/my_events.html.php

Quote:
<script type="text/javascript">
var today = Math.round((new Date()).getTime() / 1000);
window.location.href = "http://localhost/wayta2/index.php?do=/" + "&today=" + today;
</script>



This is my code and it works, meaning that I achieved my goal to assign a javascript variable into a PHP variable named $todayJS, infact the javascript variable is correctly assigned to the PHP variable $todayJS, but in this way the page get a behaviour that is not good for me.

I just want to find a way to write the file "my_events.html.php" as something like this:

Quote:
<script type="text/javascript">
var today = Math.round((new Date()).getTime() / 1000);
$.post('my_events.class.php', {today: today});
</script>


What am I doing wrong...?
the path of 'my_events.class.php'?
the sentence {today: today} ?
Something other?
Last update on March 24, 9:45 am by project-G.
Donna(data66)
#1
enclose your script code in literal tags or use $Behavior.

{literal}your script{/literal}


or use $Behavior as mentioned too:

http://purefan.blogspot.com/2012/04/using-custom-js-in-phpfox.html

I think this is what you are asking. If it's something else, please let us know.
#2
Hi Data,

I tried to perform both your advices (literal and $behaviour) but unfortunately both failed for me.

I just want to make read a javascript variable to PHP...
I posted my code above, what could be the problem?
Donna(data66)
#3
$Behavior not $behaviour
Last update on March 24, 11:16 am by Donna(data66).
#4
i wrote wrong into the post but into the code was written right: $Behavior

but anyway both solutions don't work
webwolf
#5
I am not good with javascript. I try to muddle through by referring to the internet a lot.

I do not know what your goal is so I am guessing some. If you need to do a form submission and processing, I would create a hidden input item and use js to set its value. Then you can process it with the rest of your form.

If you do not need to process a form, then you might be able to use ajax to pass the information. If this is the case, then create an ajax file /module/myevents/include/component/ajax/ajax.class.php

Note: since the underscore has a unique meaning to phpfox, I would not use it in the name of my module. For example, in the class definition, if you have "class My_events_Component_Ajax_Ajax extends Phpfox_Ajax" it might try to use "events" instead of "Component" in its parsing code, which might cause errors.

The file contents could look like:
PHP:
<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright        
 * @author          webwolf
 * @package          
 * @version         
 */
class Myevents_Component_Ajax_Ajax extends Phpfox_Ajax
{

    public function 
getDate()
    {
        
$todayJS $this->get('today');

        
$sSelect "I have received the date " date("F j, Y, g:i a",$todayJS);

        
$this->html('#js_response'$sSelect);
    }

}

?>


Then, in the template file you can have code like:

PHP:
<div id="js_response">

</
div>

{
literal}
<
script type="text/javascript">
$Behavior.todayFunction = function()
{
  var 
today Math.round((new Date()).getTime() / 1000);

  $.
ajaxCall('myevents.getDate''today=' today);
};

</script>
{/literal} 


With this code, the today variable will be sent to the server upon the page loading and ajax will process the var and return a formatted string for the div to display.

I know this is not what you need, but it may give you some ideas about altering your code.

web
Last update on March 25, 1:32 pm by webwolf.
#6
Hi Wolf!

your code has worked but there is still a little problem:

in your code you pass the javascript variable to the html template, instead I need to get it into the class file otherwise the code into the class file cannot compare the javascript variable with a database row in order to make it understand which is greater or smaller of the these two...

maybe this line can be modify in order to pass the variable to the file class instead of the html file?

Quote:
$this->html('#js_response', $sSelect);


editing that in something similar to this one:

Quote:
$this->class('#js_response', $sSelect);
webwolf
#7
Any server processing should be done within a service file. Create a file module/myevents/include/service/myevents.class.php with coding like:
PHP:
<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright        
 * @author          
 * @package          
 * @version         
 */
class Myevents_Service_Myevents extends Phpfox_Service
{
    public function 
compareDates($iDate)
    {
        
$iStoredDate $this->database()->select('mydatecolumn')
            ->
from(Phpfox::getT('myevents'))
            ->
where('user_id = ' . (int) Phpfox::getUserId())
            ->
execute('getSlaveField');

        
$bResult $iDate $iStoredDate true false;
        
$return $bResult;
    }
}    
?>


Then you can process it when it gets to the ajax file like:

PHP:
public function getDate()
    {
        
$todayJS $this->get('today');

        
$bResult Phpfox::getService('myevent')->compareDates($todayJS);

    
//Any other php code you need can be put here.

    


Again, this is just an example of a way to do this. Your circumstances might warrant a different approach.