Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

form submit from PHP code
I am new to PHP, but am an experienced programmer.
I am having trouble understanding how to
transfer control from one php/html script
to another.  I know how to let the user do it:
with links, and HTML form submits, but how do I
do it from PHP code?

e.g. if I am in script A.php , and I want script B.php to
fire up just as if it had been activated through a
form submit, with POST variables set, how do I
do it?

Matthew Nicoll



Report this thread to moderator Post Follow-up to this message
Old Post
Matthew Nicoll
09-05-04 08:56 PM


Re: form submit from PHP code
Matthew Nicoll wrote:
> I am new to PHP, but am an experienced programmer.
> I am having trouble understanding how to
> transfer control from one php/html script
> to another.  I know how to let the user do it:
> with links, and HTML form submits, but how do I
> do it from PHP code?
>
> e.g. if I am in script A.php , and I want script B.php to
> fire up just as if it had been activated through a
> form submit, with POST variables set, how do I
> do it?
>

Very basic question for which the answer can be found on php.net:

A.php:

<html>
<body>
<form method="post" action="B.php">
Name:
<input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html>


B.php:

<?
if (isset($_POST['name'])) {
print "Hello {$_POST['name']}";
}
?>


JW




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
09-06-04 08:56 AM


Re: form submit from PHP code
Matthew Nicoll spilled the following:

>
> e.g. if I am in script A.php , and I want script B.php to
> fire up just as if it had been activated through a
> form submit, with POST variables set, how do I
> do it?
>

Short answer: you can't - PHP is a server-side language.

Longer answer: If you mean you want to simulate a submit of a form to
scriptB, then it's fairly easy to do with the file(...) function (assuming
URL wrappers are enabled) e.g.
$output=get("http://server.name.com/scriptB.php?param1=something¶m2=some
thingelse");

It's a bit more tricky to do a POST (AFAIK this is not supported directly by
PHP so you need to roll your own HTTP protocol handler and open and close
sockets from your PHP code. Acutally it's not that hard (even I can do it).

HTH

C.


Report this thread to moderator Post Follow-up to this message
Old Post
Colin McKinnon
09-06-04 08:56 AM


Re: form submit from PHP code
Thanks Colin.
I was hoping I could mess with the values of <input type="hidden"...>
form variables after the submit button is clicked and before the form
action was invoked, by having the form action invoke an intermediary
php script.  I don't fancy diving into HTTP protocol handlers just
now!

... Matthew


On Sun, 05 Sep 2004 20:01:03 GMT, Colin McKinnon
<colin.thisisnotmysurname@ntlworld.deletemeunlessURaBot.com> wrote:

>Matthew Nicoll spilled the following:
> 
>
>Short answer: you can't - PHP is a server-side language.
>
>Longer answer: If you mean you want to simulate a submit of a form to
>scriptB, then it's fairly easy to do with the file(...) function (assuming
>URL wrappers are enabled) e.g.
>$output=get("http://server.name.com/scriptB.php?param1=something¶m2=som
ethingelse");
>
>It's a bit more tricky to do a POST (AFAIK this is not supported directly b
y
>PHP so you need to roll your own HTTP protocol handler and open and close
>sockets from your PHP code. Acutally it's not that hard (even I can do it).
>
>HTH
>
>C.
>


Report this thread to moderator Post Follow-up to this message
Old Post
Matthew Nicoll
09-06-04 08:56 AM


Re: form submit from PHP code
Hello,

On 09/05/2004 06:12 PM, Matthew Nicoll wrote:
>  I was hoping I could mess with the values of <input type="hidden"...>
> form variables after the submit button is clicked and before the form
> action was invoked, by having the form action invoke an intermediary
> php script.  I don't fancy diving into HTTP protocol handlers just
> now!

If you want to alter the submitted form values, just change the $_POST
array values in the beggining of the script you want to alter.

If you want to alter the values and submit them another page, you may
want to try this HTTP client class. It lets you emulate form post
submission at will, including uploading files if necessary:

http://www.phpclasses.org/httpclient


--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html

Report this thread to moderator Post Follow-up to this message
Old Post
Manuel Lemos
09-06-04 08:56 AM


Re: form submit from PHP code
Matthew Nicoll wrote:
> Thanks Colin.
>  I was hoping I could mess with the values of <input type="hidden"...>
> form variables after the submit button is clicked and before the form
> action was invoked, by having the form action invoke an intermediary
> php script.  I don't fancy diving into HTTP protocol handlers just
> now!
>

Even if you would use the HTTP protocol handlers, all that you get is the
response from the other page on the page that applies them.

If you only want to change form values before submission, you could use
javascript or apply session vars to store (some keys/values of) the $_POST
array and unwrap them on the other page.


JW




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
09-06-04 08:56 AM


Re: form submit from PHP code
Javascript is the way to go for this, I use it all the time on our Intranet
for form verification and to update hidden variables in a form.
The submitform script can be fired by any number of methods: onload,
onunload, onclick, onmouseover, onmousedown, onmouseout, etc.

<SCRIPT language="JavaScript">
var misses=0;
function submitform()
{
var nm = document.getElementById('val1').value;
if(nm==''){
misses++;
document.getElementById('val2').value='Failed to follow instructions
'+misses+' time(s)';
alert('You must enter a value');
}else{
document.myform.submit();
}
}
</SCRIPT>

<form name=myform method=post action=postit.php>
<input type=text id=val name=val1 size=50><br>
<input type=text id=val name=val2 size=50>
</form>
<input type=button onClick="submitform();" value="submit">



The submit button can be placed anywhere on the page and does not even have
to be a button, it could be text or an image, or just an area on the page
defined by a span or div.  I code for IE on our intranet so if this does not
work in all browsers then you'll have to fix it.  :)

"Matthew Nicoll" <menicoll@mars.ark.com> wrote in message
news:413b56a9.5267875@news.ark.com...
>I am new to PHP, but am an experienced programmer.
> I am having trouble understanding how to
> transfer control from one php/html script
> to another.  I know how to let the user do it:
> with links, and HTML form submits, but how do I
> do it from PHP code?
>
> e.g. if I am in script A.php , and I want script B.php to
> fire up just as if it had been activated through a
> form submit, with POST variables set, how do I
> do it?
>
> Matthew Nicoll
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Arg
09-21-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:04 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.