Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Execute PHP code inside Javascript problem
Old 03-26-2009, 08:46 AM Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Hello guys,

I have a javascript code and i need to execute php code inside that.
My code is the following:

Code:
<script type="text/javascript">
function aken() {
     $(function() {
        $("#dialog").dialog({
            bgiframe: true,
            resizable: false,
            height:180,
            modal: true,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.9
            },
            buttons: {
                'OK': function() {
                    $(this).dialog('destroy');
                    
                   //MYSQL QUERY HERE:
                   document.write("<?PHP $result = dbquery("INSERT INTO ".$db_prefix."table1 VALUES('$log_id', '".$userdata['user_name']."', NOW())"); ?>");

                    Init();

                }
                }
        });
    });

    }    
    </script>
I marked the place where i insert data into database in the code above. My example inserts correct data into database but gives javascript error after that for some reason and shows blank screen

What could be the reason and how to solve it?

Thank you in advance,
Laura
laura81 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-26-2009, 09:23 AM Re: Execute PHP code inside Javascript problem
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
javascript is clientside.
PHP is serverside.

your query will been run and finished BEFORE the javascript gets to the browser
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 03-26-2009 at 09:24 AM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-26-2009, 09:30 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Thank you, I was afraid that i have a problem like that.

But what could be the solution to achieve what i need?

Laura

Quote:
Originally Posted by chrishirst View Post
javascript is clientside.
PHP is serverside.

your query will been run and finished BEFORE the javascript gets to the browser
laura81 is offline
Reply With Quote
View Public Profile
 
Old 03-26-2009, 09:33 AM Re: Execute PHP code inside Javascript problem
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
depends on what you are trying to accomplish.

It may be that you have to handle it with an AJAX call to some server side code.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-26-2009, 09:46 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
What i try to accomplish is that when I press "ok" button in my javascript code, then data would be inserted into mysql database.

Laura
laura81 is offline
Reply With Quote
View Public Profile
 
Old 03-26-2009, 10:58 AM Re: Execute PHP code inside Javascript problem
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
In that case, like Chris said, you have to decouple the insert and the control.

Put the query in an external php page.
In your interface code (the one up there) make an ajax call to that php page that will execute the query with all the parameters you need to run the query.
Simply make the php script output something, which you will get back in the ajax request body, and parse it in javascript to give feedback to the user.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-26-2009, 11:48 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Thank you Tripy!

I can put the query to another file, but how would I call it using ajax within javascript? Maybe you could show some sample code.

I have never used ajax before, so i think its going to take ages for me to do this probably simple task.

Laura
laura81 is offline
Reply With Quote
View Public Profile
 
Old 03-26-2009, 12:13 PM Re: Execute PHP code inside Javascript problem
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Since I see you are using jQuery, you can use jQuery's native AJAX functions, which will let you do this in a line or two.

The functions will return data in a callback. Here is an example sending via a post:

Code:
$.post("/path/to/file.php", {key: "value", key2: "value2"}, function(data) {
// optional callback (do something with server response, like update the page)
});
A simple way to send a query is with $.get
Code:
$.get("/path/to/file.php?var1=whatever&var2=whatever");
Then, on the server side, you process the received data however you like.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 03-26-2009 at 12:27 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-15-2009, 02:49 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Thank you guys. I now managed to execute mysql query from the external file like Wayfarer succested. In my main file I have now the following code:

Code:
$mypage = $_SERVER['REQUEST_URI'];

<script type="text/javascript">
function My-function() {
$.post("http://localhost/planner/entry_update.php", {key: "value1", key2: "value2"}, function(data) {
// optional callback (do something with server response, like update the page)
});
}
</script>
In the file entry_update.php I have the query:

Code:
$result = dbquery("INSERT INTO ".$db_prefix."entries VALUES('$mypage', '".$userdata['user_name']."', '".$time."')");
Now i onlyu have the problem how could i pass variable "$mypage" from the main script to entry_update.php file?

Laura
laura81 is offline
Reply With Quote
View Public Profile
 
Old 04-15-2009, 04:38 AM Re: Execute PHP code inside Javascript problem
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Now i onlyu have the problem how could i pass variable "$mypage" from the main script to entry_update.php file?
Sometimes, the most obvious thing are just in froint of our eyes.

You have 2 solutions.
Either, as wayfarer told you, put them in the GET request:
Code:
$.post("http://localhost/planner/entry_update.php?mypage=something
or into the POST variables array, which is the part that says:
Code:
{key:"value1", key2:"value2"}
I imagine something like this:
Code:
$.post("http://localhost/planner/entry_update.php", {mypage: "something else"})
Now, you will need to "transfer" your php variables them to javascript, by either outputting them before the call, or by generating the ajax javascript command via php.
In the GET request:
PHP Code:
$mypage=$_SERVER['REQUEST_URI'];
 echo <<<html
 <script type="text/javascript">
function My-function() {
 $.post("http://localhost/planner/entry_update.php?mypage=
$mypage");
html; 
or via a POST request
PHP Code:
$mypage=$_SERVER['REQUEST_URI'];
echo <<<html
<script type="text/javascript">
function My-function() {
$.post("http://localhost/planner/entry_update.php", {mypage: "
$mypage");
</script>
html; 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-16-2009, 02:30 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Hello,

I tried both methods, but they did'nt work. IE shows just some javascript error.
PHP Code:
$mypage=$_SERVER['REQUEST_URI'];
echo <<<html
<script type="text/javascript">
function My-function() {
$.post("http://localhost/planner/entry_update.php?mypage=$mypage");
}
html;

or via a POST request
PHP Code:
$mypage=$_SERVER['REQUEST_URI'];
echo <<<html
<script type="text/javascript">
function My-function() {
$.post("http://localhost/planner/entry_update.php", {mypage: "$mypage");
}
</script>
html;

Now i have this kind of code that almost works:

Code:
<script type="text/javascript">
function My-function() {
var mypage = window.location.href;
$.post("/planner/entry_update.php?page=" + mypage);
}
</script>
Now the only problem is that variable "mypage" should output full url of current page. For example "http://planner/items?action=edit&id=5"
But mypage will output only "http://planner/items?action=edit" without
"&id=5"

How could I get the full url

Laura
laura81 is offline
Reply With Quote
View Public Profile
 
Old 04-17-2009, 02:49 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
Anybody???
laura81 is offline
Reply With Quote
View Public Profile
 
Old 04-17-2009, 09:25 AM Re: Execute PHP code inside Javascript problem
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
functions can't have dashes in them:
My-function()

But you're probably just making an example now that I read more closely.

Why don't you just get the "id" variable with your PHP code, then plug it into your post manually? I don't see why it shouldn't work the way it is though.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 04-17-2009 at 09:31 AM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-18-2009, 04:40 AM Re: Execute PHP code inside Javascript problem
Novice Talker

Posts: 8
Trades: 0
OK, now after trying tens of different variations i have this code:
PHP Code:
$mypage=$_SERVER['REQUEST_URI'];
 echo 
"<script type='text/javascript'>
  function My-Function() {
 $.post('/planner/entry_update.php?mypage=
$mypage');
 }
 </script>"

And it is working as the last one. Still the same problem - $mypage is passed, but the string is not complete - should be "http://planner/items?action=edit&id=5", but is "http://planner/items?action=edit", without
"&id=5"

Why does "$id=5" dissapers when passing "$mypage"

Laura
laura81 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Execute PHP code inside Javascript problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.83611 seconds with 12 queries