|
Execute PHP code inside Javascript problem
03-26-2009, 08:46 AM
|
Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
03-26-2009, 09:23 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
|
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..
|
|
|
|
03-26-2009, 09:30 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
javascript is clientside.
PHP is serverside.
your query will been run and finished BEFORE the javascript gets to the browser
|
|
|
|
|
03-26-2009, 09:33 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
|
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?
|
|
|
|
03-26-2009, 09:46 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
03-26-2009, 10:58 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
03-26-2009, 11:48 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
03-26-2009, 12:13 PM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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.
Last edited by wayfarer07; 03-26-2009 at 12:27 PM..
|
|
|
|
04-15-2009, 02:49 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
04-15-2009, 04:38 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
04-16-2009, 02:30 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
04-17-2009, 02:49 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
Anybody??? 
|
|
|
|
04-17-2009, 09:25 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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.
Last edited by wayfarer07; 04-17-2009 at 09:31 AM..
|
|
|
|
04-18-2009, 04:40 AM
|
Re: Execute PHP code inside Javascript problem
|
Posts: 8
|
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
|
|
|
|
|
« Reply to Execute PHP code inside Javascript problem
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|