form submit from PHP code

This is a discussion on form submit from PHP code within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I am new to PHP, but am an experienced programmer. I am having trouble understanding how to transfer control from ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-05-2004
Matthew Nicoll
 
Posts: n/a
Default 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


Reply With Quote
  #2 (permalink)  
Old 09-05-2004
Janwillem Borleffs
 
Posts: n/a
Default 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



Reply With Quote
  #3 (permalink)  
Old 09-05-2004
Colin McKinnon
 
Posts: n/a
Default 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&param2=somethingelse" );

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.

Reply With Quote
  #4 (permalink)  
Old 09-05-2004
Matthew Nicoll
 
Posts: n/a
Default 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.deletemeunlessU RaBot.com> wrote:

>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&param2=somethingelse" );
>
>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.
>


Reply With Quote
  #5 (permalink)  
Old 09-05-2004
Manuel Lemos
 
Posts: n/a
Default 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
Reply With Quote
  #6 (permalink)  
Old 09-05-2004
Janwillem Borleffs
 
Posts: n/a
Default 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



Reply With Quote
  #7 (permalink)  
Old 09-21-2004
Arg
 
Posts: n/a
Default 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
>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0