From: Diddum on
Hello everybody.
Until now I've only needed some php for my simple web pages, but now
I think I may need to learn javascript. Before that, I would like to
know
if the following behaviour can be obtained by means of some javascript
code

Simplifying... I have a web page (say mymain.php) in php/html with a
html form,
with just a text field and a button. Something like this:

<form method="POST" action="foobar.php" >
<input type="text" name="footxt" SIZE="8">
<input type="submit" value="gogogo">
</form>

This is the scenario that I want to change:
The user lands on this page (mymain.php), then put something in the
text field,
then clicks on the button, invoking the action page foobar.php.
Then usually the user go back (with "back" on the browser) to
mymain.php,
change the field, click the button, and so invokes again foobar.php.
And so on.
In practice, the page mymain.php is loaded once (then lies in the
browser memory)
while foobar.php is loaded with POST parameters each time the user
clicks the button.

What I want to obtain:
When the user clicks on the button, instead of loading always
foobar.php,
I want that the form invokes randomly, say, fub1.php and fub2.php.

Clearly, using server side php, I can modify easily mymain.php in such
a way that
more or less half of the users load a form with action="fub1.php" and
the
others load a page with action="fub2.php". But in this way, once
mymain.php
has been loaded, each user, going back and forth, will load always
fub1.php
or always fub2.php.
Instead I want that the same form, in some way, would lead sometimes
to fub1.php and sometimes to fub2.php.
Like, magically, the click on the button could randomly rewrite the
action target of the
form....

I apologize for this long message, but while the problem is simple, I
had some
difficulties in explaining it clearly.

thanks,
g.

From: Diddum on
Don't worry, I studied javascript for 5 minutes and I found the
solution!

Great language!

bye,
g.

From: Emily R on
Just out of curiosity, why do you want to do this?

"Diddum" <g.resta(a)iit.cnr.it> wrote in message
news:4789f4bc-8a7a-4d7c-a4a4-6a6216202efe(a)b2g2000yqi.googlegroups.com...
> Hello everybody.
> Until now I've only needed some php for my simple web pages, but now
> I think I may need to learn javascript. Before that, I would like to
> know
> if the following behaviour can be obtained by means of some javascript
> code
>
> Simplifying... I have a web page (say mymain.php) in php/html with a
> html form,
> with just a text field and a button. Something like this:
>
> <form method="POST" action="foobar.php" >
> <input type="text" name="footxt" SIZE="8">
> <input type="submit" value="gogogo">
> </form>
>
> This is the scenario that I want to change:
> The user lands on this page (mymain.php), then put something in the
> text field,
> then clicks on the button, invoking the action page foobar.php.
> Then usually the user go back (with "back" on the browser) to
> mymain.php,
> change the field, click the button, and so invokes again foobar.php.
> And so on.
> In practice, the page mymain.php is loaded once (then lies in the
> browser memory)
> while foobar.php is loaded with POST parameters each time the user
> clicks the button.
>
> What I want to obtain:
> When the user clicks on the button, instead of loading always
> foobar.php,
> I want that the form invokes randomly, say, fub1.php and fub2.php.
>
> Clearly, using server side php, I can modify easily mymain.php in such
> a way that
> more or less half of the users load a form with action="fub1.php" and
> the
> others load a page with action="fub2.php". But in this way, once
> mymain.php
> has been loaded, each user, going back and forth, will load always
> fub1.php
> or always fub2.php.
> Instead I want that the same form, in some way, would lead sometimes
> to fub1.php and sometimes to fub2.php.
> Like, magically, the click on the button could randomly rewrite the
> action target of the
> form....
>
> I apologize for this long message, but while the problem is simple, I
> had some
> difficulties in explaining it clearly.
>
> thanks,
> g.
>


From: Diddum on
On Jan 22, 3:37 pm, "Emily R" <em...(a)nospam.com> wrote:
> Just out of curiosity, why do you want to do this?

It is quite stupid.
This is why.

I have a page that performs, say, a sort of computation given an input
in a form.

In the result page I display some Google AdSense ads.

The ads of Google Adsense are sensitive to the page content. So, for
example,
if you display a page with some comments on Obama you will get ads
referring to politics.

Since my page displays the results of a computation the ads displayed
have
little content to work on and, basically, suck.

So I wanted to add some content to my result page. I do not want to
cheat adsense,
but clearly my page is a little out of this contextual-ads game. So I
want to
add some related and relevant material to the result page. More that
this, I want to experiment
different related materials in such a way that
1) adsense can associate a content to a specific web address
(like foobar1.php is a page related to politics and foobar2.php is
related to mathematics)
2) a user that repeatedly click the button on the form is going to see
both foobar1.php and
foobar2.php pages. In this way (s)he will be exposed to more varied
ads.

By the ways, this is more of less a pastime to force myself to learn
some
php, css, and hopefully some javascript, since my earnings with ads
amount to less than
2 daily bucks...

Probably there is a more clever way to do this, but I simply
created a function named randaction() which get a handle on the form
and randomly modify the action field. Then I added a
onClick="randaction()" to my
input form.

This seems to works. In practice the onclick seems to change on-the-
fly the action field
of my form just before it is used to jump away.

bye,
g.

From: Scott Sauyet on
On Jan 22, 7:57 am, Diddum <g.re...(a)iit.cnr.it> wrote:
> What I want to obtain:
> When the user clicks on the button, instead of loading always
> foobar.php,
> I want that the form invokes randomly, say, fub1.php and fub2.php.

Do you want to consider users who do not have Javascript available and
on at your site? They're a pretty small minority, but they certainly
exist. If you did this on the server, it work for everyone.

This is a forum about JS, and I'm glad you found a JS solution, but it
should be pretty simple to do in PHP:

<?php
if (rand(0, 1) == 1) {
include("fub1.php");
} else {
include("fub2.php");
}
?>

Good luck,

-- Scott