Passing parameters form a JS page to a PHP page

Javascript coding ..

Moderators: macek, egami, gesf

francois
New php-forum User
New php-forum User
Posts: 4
Joined: Thu May 27, 2004 1:57 am

Passing parameters form a JS page to a PHP page

Postby francois » Thu May 27, 2004 4:05 am

On the JS page file, I have:
<script language="JavaScript"
src="ecriture21.php?parm1=" + page>
</script>
the last of numerous other trials.

On the called PHP page, I have:
$page=$_GET['parm1'];

This gives nothing, the $page is empty.

Would the Parm1 value be a constant, the matter would be simple; I made this work easily.

The difficulty here comes from the variable Page value.

Thanks in advance.

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Thu May 27, 2004 10:32 am

Well, the javascript code you´re using is to load an external .js file to your page. So it´s kindly passing nothing through url!

Use this one!
You ´ll print/use it where/when you need to send some url, or the url to be replaced with:

Code: Select all

<script language="JavaScript">
window.location.replace("http://full/url/to/ecriture21.php?parm1=");
</script>


This is like using php's header() function!

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Re: Passing parameters form a JS page to a PHP page

Postby ruturajv » Thu May 27, 2004 7:39 pm

francois wrote:

Code: Select all

   <script language="JavaScript"
   src="ecriture21.php?parm1=" + page>
   </script>



Is your javascript variable 'page' have any contents... ?

francois
New php-forum User
New php-forum User
Posts: 4
Joined: Thu May 27, 2004 1:57 am

Postby francois » Tue Jun 01, 2004 2:11 am

Early testing show, Jesf, that your solution works nicely; I want to thank you for your answer.

Let me ask one more question about the way I started with:
- to what usage would this way of calling an outside JS function work?
- is it to call JS code rather than PHP code?
- is it that this woul not work anyway, even for JS code?

Thank you in advance for your answer.

F

francois
New php-forum User
New php-forum User
Posts: 4
Joined: Thu May 27, 2004 1:57 am

Postby francois » Tue Jun 01, 2004 4:13 am

Jesf

As a second thought, further away in testing, let me restate my need:
- a front page passes parameters to a back page
- the back page executes some process (mySQL write) but does not display anything

In my need, the front page does all the display, before calling the back page and after the later returned control to the former; actually, I don't need to replace the first page by the second one even though I could live with it.

To stick with your solution, it remains to find a way to return from the back page (PHP) to the front page (no passing parameters).

If possible; thank you in advance.

F

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Tue Jun 01, 2004 6:42 am

Buááá! Don´t do me that! Isn´t Jesf... but Gesf...!!

Joke!
Ok, that code you were using is to call a external .js file!
Why!? And... what usage would have!?
Well, theres no difference having the code in a external file or inside the html code, but if you have a lot of javascript code... it would be cleaner/better having it in another file!

Yes, it is to call javascript code.
Never forget this: Javascript is client-side scripting, unlike php witch is server-side scripting!
However, it´s possible to 'interact' with both (not directly), anyway you need to understand this concept and how it could be done.
Let´s say you would use php to do all you have to do before the page is loaded/shown!

For your particular case (sending stuff through url), let´s see two differente examples!

In this first, we will set a variable in server to be catch by javascript when the page is 'shown':

Code: Select all

<?php
$name = 'Maria';
?>

// Will print Maria
<script language="JavaScript">
  document.write("<?php echo $name; ?>");
</script>

<?php
// Contunie with the php code
?>


In this next, we will send a variable through url with javascript, to be catch by php when the pages gone to be loaded in server (example):

Code: Select all

<?php

/*
This php code will be loaded just if a variable section is passed through url, don´t matter the scripting language!
*/

if(isset($_GET['word'])){
echo $word; // Output maria
}
?>

<script language="JavaScript">
  window.location.replace("http://www.yoursite.com/thisfile.php?word=maria");
</script>

// Contunie with code


Well, hope you understand, maybe this isn´t the best example.
See this related post !

Anyway, for your last post, can you show/tell me what you´re planning to do!?

Cheers,
Gesf

francois
New php-forum User
New php-forum User
Posts: 4
Joined: Thu May 27, 2004 1:57 am

Postby francois » Thu Jun 03, 2004 12:07 am

hi Gesf

My apology first for mistyping your name.

Thank you for your two examples of passing parameters between JS and PHP.

In my particular case, I needed to valorize JS parms before writing them into a mySQL database.

The trouble I was into was that, leaving it all in one page, these JS parms were not initialized before PHP would interpret the page.

I then went to two distinct pages, the front one which does the display and the back one which does only the PHP-mySQL writing with absolutely no display + return to the front page.

This is were I needed to pass these parms between the front and the back pages.

Here is the solution (Yes, it works) that I found somewhere else pretty recently:
<script type="text/javascript">
document.write('<script type="text/javascript" '
+ 'src="ecriture21.php?parm1='
+ page
+ '&parm2='
+ resolution
+ '">'
+ '</'+'script>');
</script>
shown here for 2 parms; tehre is the classical GET stuff in the back page.

Hope this helps; would anyone have a different solution (all in one page?), welcome to it

F


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest

cron