Shoutbox

javascript POST? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: javascript POST? (/showthread.php?tid=21608)

javascript POST? by bach_m on 02-20-2004 at 03:02 AM

i'm looking for a javascript box to popup and ask for a name, and then pass that to my website as POST data, for use in a PHP script. can it be done??? i dont know the first thing about PHP...


RE: javascript POST? by WDZ on 02-20-2004 at 04:32 AM

Why can't you use GET? I think that would be easier... :-/

I'm not great at JavaScript, but I think the only way to pass a value via POST is to make a form in your page with a hidden field. You can then set the field to the value returned by the JS prompt function, and submit the form using JavaScript.

(I'm not going to post example code... not enough time :p)


RE: javascript POST? by fluffy_lobster on 02-20-2004 at 09:31 AM

You can also do it without changing the main page's navigation by using an iframe.  I'll do a quick dummy example:

First you make a page with your form on it, say it was called hiddenform.htm:

code:
<form name="hiddenForm" method="POST" target="myurlwhichtakespostdata.php"><input type="hidden" id="variableToSend" value=""></form>
Once you've done that, you can stick it on the following page with all the relevant javascript:
code:
<script language="Javascript"><!-- function sendData(theData){
document.all.myiFrame.getElementById('variableToSend').value = theData ;
document.all.myiFrame.forms['hiddenForm'].submit() ;
}
-->
</script>


<!-- All your page scripting here, run the javascript function sendData('whatever') when you wanna send the data -->

<iframe src="hiddenform.htm" frameborder="0" scrolling="NO" width="1" height="1"></iframe>
And you're done :P (Y)
RE: javascript POST? by CookieRevised on 02-20-2004 at 10:55 AM

There is no need for an iframe at all if you just want to popup a inputbox and then redirect the current page to your website... If you do want that the main page (where the popup is launched) to stay, then you can use Lobster's method...

So, it depends what you want.
Do you want that popup at you website which also takes the data (name)?
Do you want to be redirected to your website after the name is filled out?
Is it nessecary to use the POST method? Can't you use the GET method?
Can you be more clear about what you exactly to happen and where it should happen (in clear steps)?

for example:
(the user will not know what happens with his name after he filled it in.)
1) User opens page A
2) a popup is shown asking for his name and the data is send to page B (your website)
3) the user stays on page A

or for example:
(the user is redirected to the page which processes the name.)
1) User opens page A (your website)
2) a popup is shown asking for his name and the data is send to page B
3) that page B processes the name and outputs some data accordingly. For example: "Hi, <name>. Welcome at my website"


RE: javascript POST? by KeyStorm on 02-20-2004 at 11:57 AM

If you know sessions you can use them, they're very confortable. Although I don't really know how to manage them :grin:

Really interesting for what you want to do. :)


RE: javascript POST? by bach_m on 02-20-2004 at 12:04 PM

quote:
Originally posted by CookieRevised

(the user is redirected to the page which processes the name.)
1) User opens page A (your website)
2) a popup is shown asking for his name and the data is send to page B
3) that page B processes the name and outputs some data accordingly. For example: "Hi, <name>. Welcome at my website"

this one..

i'd prefer to not divulge WHY i need it. but i need it to be processed by a PHP script. for instance. they open a page. checks whether thay have a cookie saying that they have given their name (and what ;)), and if not, then it pops up a box saying"whats ur name?", and then submits that to my main page, which proceces that.

i'd prefer not to use get, but if absolutely necesary, i guess i can

RE: javascript POST? by CookieRevised on 02-20-2004 at 01:41 PM

It still isn't exactly clear what you want to do. Explain in clear steps and give each page/link a name ;)

But IF I read you correctly then with GET-method this is very easy todo...

in the page A you have to make a script which:
1) Check for cookie
2) if exist then reload page with page C
3) if it doesn't exist, ask for name and reload with page B

C and B can be the same page actually if you check in that page if something is added to the URL.

Even more: A, B and C can be the same page if you use this structure:
1) load the page
2) check if a cookie exist
4) if it exist, read it/process it load the page further...
3) if not then ask for a name, create the cookie, and load the page further...

There is no need for a form if you do that. All this can be done in plain javascript (or convert everything to php if you like). In Javascript it would be something like this:

code:
<html>
  <head>
    <title>Main Page</title>
    <script language="JavaScript">
      // Get the users name from a cookie,
      // if it doesn't exist then ask for it and create the cookie.
      var username = unescape(document.cookie)
      username = username.split("=")
      if (username == "") {
        username = prompt("What is your name?","")
        var expiredate = new Date()
        expiredate.setMonth(expiredate.getMonth() + 1)
        document.cookie = "Name=" + username + ";expires=" + expiredate.toGMTString() + ";path=/"
      }
      //
      // put your processes here, the variable username will hold the value of the name.
      //
    </script>
  </head>

  <body>
    normal page
  </body>
</html>

not tested
RE: javascript POST? by KeyStorm on 02-20-2004 at 02:45 PM

quote:
Originally posted by bach_m
then it pops up a box saying"whats ur name?", and then submits that to my main page, which proceces that.
Sessions are perfect for this kind of thing:
That's variabales that are stores in the server and are kept alive while the session is alive (like in these forums) and they can be accessed from any php in the server.
So you can store the data from your pop-up to the session-vars and retrieve it from the main-page. All this occurs in the darkness so nobody has control over it (php-sessions is the most secure way to send and recieve data).
RE: javascript POST? by WDZ on 02-20-2004 at 03:39 PM

quote:
Originally posted by KeyStorm
Sessions are perfect for this kind of thing:
I dunno about that... seems kinda overkill... :p

If you're just passing one variable to one other page, you should just use the JavaScript POST thing as shown above. Even if you used sessions, you'd still have to do the same thing to get the value from JavaScript to PHP.

Also, sessions expire (default time is like half an hour), and are designed to exist only during one visit to the site. If you want your users' data to still be available when they return tomorrow, you should use cookies. If for some reason you don't want the data stored in cookies, store it in a database with some kind of ID associated with it, then put the ID in a cookie.
RE: javascript POST? by KeyStorm on 02-20-2004 at 04:19 PM

Remember you can read and set cookies in PHP ;)


RE: javascript POST? by WDZ on 02-20-2004 at 05:10 PM

quote:
Originally posted by KeyStorm
Remember you can read and set cookies in PHP ;)
Who are you replying to? As my post is right above yours, and above my post is another one of your posts, I'll assume you're replying to me, but that doesn't make sense... I'm well aware of the fact that PHP can read and set cookies... :p
RE: javascript POST? by KeyStorm on 02-20-2004 at 05:13 PM

quote:
Originally posted by WDZ
I'm well aware of the fact that PHP can read and set cookies... :P
Obviously ;)

Well... Sessions are very useful for defining and logging in, similar to what bach wants to do. But... yeah, maybe overkill :P
RE: javascript POST? by bach_m on 02-20-2004 at 08:21 PM

quote:
Originally posted by KeyStorm


Well... Sessions are very useful for defining and logging in, similar to what bach wants to do. But... yeah, maybe overkill


i didn't say they would log in ^o)

i wanna get their name and then store it in a batabase along with their IP address :D:D


but i think i'm just too lazy to want to do that :P
RE: javascript POST? by CookieRevised on 02-20-2004 at 09:25 PM

Well, use the method I described, but convert it into PHP and put it on top of your php page. And instead of a cookie, use the database.... It can't be simpler then that...

Get_Current_IP
if (Current_IP isin Database) {
  Get_Name_Out_of_Database
} else {
  Ask_Name
  Store_Name_In_Database
  Store_Current_IP_In_Database
}

print_name_with_some_welcome_msg
do_rest_of_page
yadda_yadda
:D


RE: javascript POST? by bach_m on 02-20-2004 at 11:20 PM

and just use ur script for getting the name?? makes sence....

or shud it just be a form?? such as:

code:
<?php
look_for_IP_in_database;
if not there, pop up form as a web page;
   once submitted, procede to main page.
if yes there, then popup the main page;
?>


and cookie. u forgot the semicolons :P
RE: javascript POST? by CookieRevised on 02-21-2004 at 08:12 AM

No, you don't need a form to do what you want I think. Unless you can't popup an inputbox with PHP to get a name from the user. I don't know PHP. It was just an workflow idea how you could do it in pure javascript, which you can convert to PHP I thought. But you need a function in PHP where you can popup an inputbox for it to work...


Here is the javascript version of it. This time I tested it, and as you can see, not much had to be changed (only had to add [1] and replace "" with null):

code:
<html>

  <head>
    <title>Main Page</title>
    <script language="JavaScript">
      function GetName() {
        var username = unescape(document.cookie)
        username = username.split("=")[1]
        if (username == null) {
          username = prompt("What is your name?","")
          var expiredate = new Date()
          expiredate.setMonth(expiredate.getMonth() + 1)
          document.cookie = "Name=" + username + ";expires=" + expiredate.toGMTString() + ";path=/"
        }
        return username
      }
      function ClearName() {
        var expiredate = new Date()
        document.cookie = "Name=;expires=" + expiredate.toGMTString() + ";path=/"
      }
    </script>
  </head>

  <body onLoad="javascript:alert(GetName() + ', welcome...');">
    <input type="button" value="Clear cookie..." onClick="ClearName()">
    <br>main page
  </body>

</html>
So this is the concept, I guess. I'm sure this can be done in PHP also, but instead of cookies, use a database... BTW, this is only a very crude example.


PS: about the semicolons, you don't need them in javascript. But it is indeed a good habbit to put them there...
RE: javascript POST? by KeyStorm on 02-21-2004 at 12:33 PM

quote:
Well, use the method I described, but convert it into PHP and put it on top of your php page. And instead of a cookie, use the database.... It can't be simpler then that...

The IP's are dynamical... In many cases more unexpectable than cookies.

Cookies can be deleted by the user, but the user has no control over the IP's. Some countries may have fixed IP's, but, as the number of free IP's is becoming very little (while IPv6 is not implemented) they are rapidly changing to the dynamic IP connection. In Spain for example (I'm sure many other countries do so) your IP can be changed even while you're online (when the modem/router notices there is no connection through its gateway it reports itself as idle to the ISP and the ISP may change its IP)

So, the best solution is to set cookies, because normal people (no nerds like us ;)) do not keep deleting their cookies constantly (well, neither do I)