/    Sign up×
Community /Pin to ProfileBookmark

Can PHP automatically submit forms like Javascript.

I have a page with a list of products (products.htm). When the user clicks the buy now button it opens another page (page2.php).

In page two I have imported through hidden variables all the variables on products.htm (using $_POST). A script will then determine which company was selected…

If Company A was selected I want to submit a form to open a new page called CompA.php (importing all the same hidden variables)

If Company B was selected I want to submit a form to open a new page called CompB.php (importing all the same hidden variables)

And so on…

I have got it working except for submitting a new form (CompA.php/CompB.php). I tried something like this:

[code]
echo “<form method=’post’ action=’/CompA.php’>”;
echo “<input type=’hidden’ value=’$_POST[“CompanyName”]’>”;
echo “<input type=’hidden’ value=’$_POST[“Product”]’>”;
echo “document.this.form.submit()”;
echo “</form>”;
[/code]

it doesn’t submit the form but… if I make an active submit button then it does work…

What I want to know is how to force a submit like in javascript..

Can anyone help me???

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJan 01.2005 — why not do a redirect on page2.php, instead of re-submiting the data, for example:[code=php]<?php
if($_POST)
{
if(!empty($_POST['CompanyName']))
{
switch($_POST['CompanyName'])
{
case 'CompA':
$url = 'CompA.htm';
break;
// and so on ...
}
header("Location: {$_SERVER['HTTP_HOST']}/{$url}");
exit;
}
else
{
exit("you did not select a Company");
}
}
else
{
exit("no data was submitted");
}
?>[/code]
Copy linkTweet thisAlerts:
@zincoxideauthorJan 01.2005 — I think that will work...

I am not familiar with php tho... I don't need to have a safety as far as "no company selected". The only way they would get to this page is if they click the buy now button which, the way everything is set up always has data in it.

What I want to set up is:

If company a goto this page

if company b goto this page

if company c goto this page

if company d goto this page

if any other company goto a generica page


The only reason why I had "Worked and Didn't Work" is because I was trying to figure out how to do it.

If I did this would it work???

<i>
</i>&lt;?php
if($_POST)
{
switch($_POST)
{
case 'CompA':
$url = 'CompA.htm';
break;
// and so on ...
}
header("Location: {$_SERVER['HTTP_HOST']}/{$url}");
exit;
}
else
{
switch($_POST)
{ $url = 'Generic.htm'; }
}
?&gt;


I don't know something like that???

Also what does this do:

<i>
</i> header("Location: {$_SERVER['HTTP_HOST']}/{$url}");
exit;
Copy linkTweet thisAlerts:
@zincoxideauthorJan 01.2005 — On second thought, will that "drag the $_POST array into CompA.htm"???


This is what I need to do.
Copy linkTweet thisAlerts:
@ShrineDesignsJan 01.2005 — you will need to change and/or add to this in order for it to work: case 'CompA':
$url = 'CompA.htm';
break;
this is a server-side redirect: header("Location: {$_SERVER['HTTP_HOST']}/{$url}");
exit;
Copy linkTweet thisAlerts:
@phpnoviceJan 01.2005 — [i]Originally posted by ShrineDesigns [/i]

[B]this is a server-side redirect:<i>
</i>header("Location: {$_SERVER['HTTP_HOST']}/{$url}");
exit;
[/B][/QUOTE]

I was of the understanding that a "complete" URL was required for the [b]Location[/b] header:
<i>
</i>header("Location: http&amp;#58;//{$_SERVER['HTTP_HOST']}/{$url}");
exit;
Copy linkTweet thisAlerts:
@ShrineDesignsJan 01.2005 — a mistake on my partheader("Location: http://www.{$_SERVER['HTTP_HOST']}/{$url}");
exit;
orheader("Location: /{$url}");
exit;
for HTTP 1.1 an absolute uri is required, and for HTTP 1.0 a relative uri is allowed
Copy linkTweet thisAlerts:
@zincoxideauthorJan 01.2005 — Okay... But... Will it drag in the $_POST array into the destination page. This is absolutely crucial for me.
Copy linkTweet thisAlerts:
@phpnoviceJan 01.2005 — No, sorry, it will not. Generally, you would have to reformat the POST data into a query string (GET-type data) and attach it to the redirected URL.
Copy linkTweet thisAlerts:
@ShrineDesignsJan 01.2005 — [i]Originally posted by zincoxide[/i]

[B]Okay... But... Will it drag in the $_POST array into the destination page. This is absolutely crucial for me. [/B][/QUOTE]
why bother, html files can't parse post data
Copy linkTweet thisAlerts:
@phpnoviceJan 01.2005 — [i]Originally posted by zincoxide [/i]

[B]<i>
</i>echo "&lt;form method='post' action='/CompA.php'&gt;";
echo "&lt;input type='hidden' value='$_POST["CompanyName"]'&gt;";
echo "&lt;input type='hidden' value='$_POST["Product"]'&gt;";
echo "document.this.form.submit()";
echo "&lt;/form&gt;";


it doesn't submit the form but... if I make an active submit button then it does work...

What I want to know is how to force a submit like in javascript..[/B][/QUOTE]

To force a submit, you would modify the BODY tag as follows -- e.g.:
<i>
</i>&lt;body onload="document.forms[0].submit();"&gt;

Keep in mind that the client must have JavaScript enabled, though.
×

Success!

Help @zincoxide spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...