Solved

New to xajax/javascript = problem

Posted on 2009-07-11
10
426 Views
Last Modified: 2013-11-11
Hello!

I'm trying to put a javascript variable into a PHP variable using xajax. But I can´t get it to work.

Error msg:

Fatal error: Class 'xajaxResponse' not found in /home/web9905/domains/domain.com/public_html/xajax/multiply.php on line 23

What am I doing wrong?
<?php
require("xajax/xajax_core/xajax.inc.php");
echo '<?xml version="1.0" encoding="UTF-8"?>'
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title></title>
        <script language="javascript">
            var testVar = 5;
        </script>
 
 
    </head>
    <body>
 
        <?
        MyPHPFunction(testVar);
 
        function MyPHPFunction($jsVar) {
            $variable = strtoupper($jsVar);
            $objResponse = new xajaxResponse();
            $objResponse->script("PHPvar = '$variable';");
            return $objResponse;
		}
        ?>
 
        <div id="someID"><p></p></div>
    </body>
</html>

Open in new window

0
Comment
Question by:AphX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24832811
You need to initialize xajax. Put this after you include xajax on line 2:

$xajax = new xajax();
$xajax->registerFunction("MyPHPFunction");
$xajax->processRequest();

Within HTML HEAD you need this:

<?php $xajax->printJavascript(); ?>

This sets up the xajax javascript helper functions.
0
 
LVL 2

Author Comment

by:AphX
ID: 24832839
Hi again and thank you! :)

Now I get this pop-up error msg:

Error: the xajax Javascript component could not be included. Perhaps the URL is incorrect? URL: xajax_js/xajax_core.js

I dont know why =(

the require path at line 2 is correct... one of the xajax example's worked so there is no file missing in the framework.
<?php
require("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
$xajax->registerFunction("MyPHPFunction");
$xajax->processRequest();
echo '<?xml version="1.0" encoding="UTF-8"?>'
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title></title>
        <script language="javascript">
            var testVar = 5;
        </script>
        
        <?php $xajax->printJavascript(); ?>
    </head>
    <body>
        
        <?
        MyPHPFunction(testVar);
        
        function MyPHPFunction($jsVar) {
            $variable = strtoupper($jsVar);
            $objResponse = new xajaxResponse();
            $objResponse->script("PHPvar = '$variable';");
            return $objResponse;
        }
        ?>
        
        <div id="someID"><p></p></div>
    </body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:AphX
ID: 24832841
If I remove line 17, the msg doesn't show up...
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24832885
Line 17 must be there.

Because xajax is not stored directly in the folder where your script is located, you need to configure the path. Try this:

$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
$xajax->registerFunction("MyPHPFunction");
$xajax->processRequest();

 
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24832906
Your line 22 is wrong... you are calling the PHP function from PHP.

Put this in the HTML instead:

<button onclick="xajax_MyPHPFunction('Hello world!');">Test</button>
<button onclick="alert(PHPvar);">Show result</button>

Also, line 21 should be "<?php", not just "<?". It might work on your server, but some servers does not accept the short start tag.
0
 
LVL 2

Author Comment

by:AphX
ID: 24832947
Thanks a lot! All of that worked like a charm!

Last thing...

I have a lot of PHP code, how do I actually put the testVar variable to a PHP variable?

Sorry for being a noob!

Your help really means a lot to me!
<?php
require("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
$xajax->registerFunction("MyPHPFunction");
$xajax->processRequest();
echo '<?xml version="1.0" encoding="UTF-8"?>'
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title></title>
        <script language="javascript">
            var testVar = 5;
        </script>
        
        <?php $xajax->printJavascript(); ?>
        
        <script language="javascript">
		var test = 5;
        </script>
 
    </head>
    <body>
        
        <?php
        
        function MyPHPFunction($jsVar) {
            $variable = strtoupper($jsVar);
            $objResponse = new xajaxResponse();
            $objResponse->script("PHPvar = '$variable';");
            return $objResponse;
        }
        ?>
        <button onclick="xajax_MyPHPFunction('testVar');">Test</button>
		<button onclick="alert(PHPvar);">Show result</button>
        <div id="someID">
        	<?php
				$varFromJs = "?";
			?>
        </div>
    </body>
</html>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24832964
Just remove the quotes:

<button onclick="xajax_MyPHPFunction(testVar);">Test</button>

...but now the strtoupper() PHP function does not make much sense... for testing, change it like this:

$variable = $jsVar * 2;

...and remove the quotes in the PHP response, because it is now an integer, not a string:

$objResponse->script("PHPvar = $variable;");
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24832968
To write to the someID div, add this to the response object:

            $objResponse->assign('someID','innerHTML',"Result: $variable");
0
 
LVL 2

Author Comment

by:AphX
ID: 24833011
I know these are strong words... But. I LOVE YOU!!! <3

You just helped me complete a 1-month project.

I really need to practice JavaScript :)

Thank you thank you thank you!

You're my god!
0
 
LVL 2

Author Closing Comment

by:AphX
ID: 31602501
!!! You're really a Genius
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question