Passing form values from html to php function

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 20822 Views - Last Post: 21 June 2011 - 10:44 AM Rate Topic: -----

#1 mega01man  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Passing form values from html to php function

Posted 16 June 2011 - 11:41 AM

I've been searching but I don't see anything I'm trying to do. And I'm starting to think that its not possible.

I've been trying to pass form values from HTML to a PHP function.

This is the sample slice of my codes:

Part A: Registration form
<form action="confirmation.html" method="post">
<table summary="registration form">
<tr>
<td><label for="username">Username:</label></td>
<td><input type="text" id="username" name="username" size="25" maxlength="25" /></td>
</tr>
<tr>
<td><label for="password">Username:</label></td>
<td><input type="password" id="password" name="pass" size="25" maxlength="8" /></td>
</tr>
</table>
</form>



Part B: Confirmation page
<?php registration(); ?>

then some html for output.



Part C: registration.php file
<?php
function registration($userName, $passWord) {
$userName; $passWord;
return $userName; return $passWord;
?>



I even tried:
<?php registration('username', 'pass'); ?>
even without- '



Also in php file I added this line before the returns:
echo $userName." ".$passWord;


but all I get is a blank page.

I'm trying to code my site's pages purely out of .html and just call the php scripts from a php file. Is this possible?

I've also read about $_GET but it passes the values via URL which I read can cause security issues.

Thank you very much!

This post has been edited by Dormilich: 17 June 2011 - 03:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing form values from html to php function

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Passing form values from html to php function

Posted 16 June 2011 - 12:02 PM

<?php
function registration($userName, $passWord) {
$userName; $passWord;
return $userName; return $passWord;
?>




This code is nonsense. What are you trying to do here?

<form action="confirmation.html" action="post">

Your form action needs to point to a PHP page.
Your variables will be POSTed to the PHP page and will be available as $_POST['username'] and $_POST['pass'].

I think you need to spend some time with the PHP manual. Here's the part of the tutorial on forms. In addition we have a lot of tutorials here.
Was This Post Helpful? 1
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: Passing form values from html to php function

Posted 16 June 2011 - 12:31 PM

View Postmega01man, on 16 June 2011 - 02:41 PM, said:

I've also read about $_GET but it passes the values via URL which I read can cause security issues.


Apart from the fact that you need to submit your form to a PHP script GET methods are only a security issue if you the programmer doesn't validate or filter the input.

Example
http://www.yourdomain.com/script.php?id=5

mysql_query("SELECT * FROM `table` WHERE id = {$_GET['id']}");

This proves to be a big security flaw because it opens your database to SQL injections. Since we need an integer to validate this input we'll only accept integers.

$_POST is just as vulnerable too so not just because we can't see post data doesn't mean we can't send custom generated POST requests to a script.
Was This Post Helpful? 1
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Passing form values from html to php function

Posted 16 June 2011 - 12:48 PM

View Postcodeprada, on 16 June 2011 - 07:31 PM, said:

Apart from the fact that you need to submit your form to a PHP script GET methods are only a security issue if you the programmer doesn't validate or filter the input.

There is one thing that should be considered though. GET values are a part of the URL, and URLs are often cached by browsers or even nodes along the route between the server and client, whereas entire POST requests may not be. - I mean, you wouldn't want a URL containing your bank account's pin number, or something along those lines, to be stored in your browser's history, or in some random ISP router cache between you and your bank.

Unencrypted POST request are no safer from prying eyes, that's true, but what I'm talking about is more of an benign, non-intentional security threat.
Was This Post Helpful? 1
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: Passing form values from html to php function

Posted 16 June 2011 - 12:58 PM

These days no matter how it leaves the browser or computer it's vulnerable to prying eyes. SSL\VPN connections are the best and most secure way to communicate these days. Sending data securing is another big topic all together.
Was This Post Helpful? 0
  • +
  • -

#6 WantToBeJavaGuru  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 08-June 11

Re: Passing form values from html to php function

Posted 16 June 2011 - 09:36 PM

Hello great question

It's been some time since iv'e studyed php and put in into practice.
This is what I can remeber I hope it helps at the least in some small way to achieve your desigered goal.


key components:

1. The HTML form tag must have the action attribute and a post value example: <form action="post" >

2. each input tag or label I forget sorry. must have a name attribute example: <input type="text" name="">
(php can now look at the name attributes of your input fields and identify whats is what.)

3. you must creat a session at very top of page before any html code, reasearch (php sessions) sorry i cant help more here.

4. with in you session code block, you can now capture the entered html field data. by assignment like this.
example:
$name = $_POST["name"]
$email = $_POST["email"]
and so forth.

5. i think and i hope you can now refer to these variables, extracting the texfield datat each time the form is used :)

NOTE: A php session must be active to keep variable data.
The minute a session is destroyed or the browser is closed the php session is destroyed and variables are nullified.


Send me an email if this helps, or any other problems you may have.

Best of luck
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Passing form values from html to php function

Posted 17 June 2011 - 12:37 AM

@WantToBeJavaGuru It seems you've forgotten more than you realize :)

View PostWantToBeJavaGuru, on 17 June 2011 - 04:36 AM, said:

1. The HTML form tag must have the action attribute and a post value example: <form action="post" >

That's incorrect. A form attribute must have an action attribute that points to whichever page is meant to receive the data, and optionally a method attribute that specifies the data transfer method: POST or GET. (It defaults to GET if it's not specified.)
<form action="doStuffWithData.php" method="post">


View PostWantToBeJavaGuru, on 17 June 2011 - 04:36 AM, said:

2. each input tag or label I forget sorry. must have a name attribute example: <input type="text" name="">
(php can now look at the name attributes of your input fields and identify whats is what.)

Input tags must have valid name values, but the labels don't. A label tag has a for attribute that points to the id of the input it belongs to.

View PostWantToBeJavaGuru, on 17 June 2011 - 04:36 AM, said:

3. you must creat a session at very top of page before any html code, reasearch (php sessions) sorry i cant help more here.

A session has nothing to do with passing form data. If you want to use a session, then yes you must initialize it before printing anything, but it's not required to accept data from a form.

View PostWantToBeJavaGuru, on 17 June 2011 - 04:36 AM, said:

4. with in you session code block, you can now capture the entered html field data. by assignment like this.
example:
$name = $_POST["name"]
$email = $_POST["email"]
and so forth.

Again, sessions have nothing to do with this. Also, it's generally a bad idea to assign request values to normal PHP variables without verifying and sanitizing them first. Easier to slip up and let unfiltered user input through.

View PostWantToBeJavaGuru, on 17 June 2011 - 04:36 AM, said:

NOTE: A php session must be active to keep variable data.
The minute a session is destroyed or the browser is closed the php session is destroyed and variables are nullified.

A PHP session only keeps data that is deliberately put into the session. It won't preserve normal variables.
<?php
session_start();

if (isset($data1)) { 
    // This will never print, as it resets each request.
    echo $data1; 
}
else {
    $data1 = time();
}

if (isset($_SESSION['data2'])) {
    // But this will print the value assigned to it first time
    // the script was executed.
    echo $_SESSION['data2']; 
}
else {
    $_SESSION['data2'] = time();
}


Was This Post Helpful? 1
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Passing form values from html to php function

Posted 17 June 2011 - 01:40 AM

View PostWantToBeJavaGuru, on 17 June 2011 - 06:36 AM, said:

1. The HTML form tag must have the action attribute and a post value example: <form action="post" >

yes and no. action is required but gets the processing script’s URI. method is what you set to get/post (defaults to get)


View PostWantToBeJavaGuru, on 17 June 2011 - 06:36 AM, said:

2. each input tag or label I forget sorry. must have a name attribute example: <input type="text" name="">
(php can now look at the name attributes of your input fields and identify whats is what.)

PHP can’t look at the input fields. the form data collection is done on the browser and PHP only receives that pre-processed data. those data are sent as key-value pair (name=value).
Was This Post Helpful? 1
  • +
  • -

#9 WantToBeJavaGuru  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 08-June 11

Re: Passing form values from html to php function

Posted 17 June 2011 - 06:46 AM

My Sincere apoligies. The last thing I want to do is misguide anyone.
Was This Post Helpful? 0
  • +
  • -

#10 mega01man  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: Passing form values from html to php function

Posted 17 June 2011 - 08:57 AM

Thank you all for your help.

I noticed that I typed
action="post"
instead of
method="post"
. I am very sorry for that and I'm going to change it now if its possible.

I also did

<form action="registration.php" method="post">
<table>
<tr>
<td><label for="username">Username:</label></td>
<td><input type="text" id="username" name="username" size="25" maxlength="25" /></td>
</tr>
<tr>
<td><label for="password">Password:</label></td>
<td><input type="text" id="password" name="password" size="25" maxlength="8" /></td>
</tr>
</table>



And this is the php file:
<?php

$username = $_POST['username'];
$password = $_POST['password'];

echo $username."<br/>";
echo $password."<br/>";

?>


This worked. It produced the output letting me know that the values are passed.


I know there are security issues in my codes but I'm slowly attending to it. I just began studying php.
And this is my first personal project for myself. I've been studying passing values to php file in the form attribute - action. But I want to expand on that I bit that's why I tried to pass the value to an html file.

I also tried a method embedding the php code in the html file and it work.
This is what I did:
in the html file
<form action="registration.html" method="post">
<table>
<tr>
<td><label for="username">Username:</label></td>
<td><input type="text" id="username" name="username" size="25" maxlength="25" /></td>
</tr>
<tr>
<td><label for="password">Password:</label></td>
<td><input type="text" id="password" name="password" size="25" maxlength="8" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="submit" />
</tr>
</table>



and the registration html file:
<?php
$username = $_POST['username'];
$password = $_POST['password'];

echo $username."<br/>";
echo $password."<br/>";
?>


This also worked.

But that's not what I wanted it to do.
I want to pass the values to the php file and just call them in the html file via function when the registration.html is called by the submit button in the form.

Again thank you very much for your time and help!

Update: I can't edit my primary post to change the
action="post"

It should be
method="post"

This post has been edited by mega01man: 17 June 2011 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#11 Rhokk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 17-June 11

Re: Passing form values from html to php function

Posted 17 June 2011 - 09:08 AM

What about the php file confuses you? You're echoing the username and password from the post array...Which is being passed from the HTML form.

This post has been edited by Rhokk: 17 June 2011 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: Passing form values from html to php function

Posted 17 June 2011 - 10:38 AM

PHP code is suppose to be in PHP files. Basically you want to place some values processed in the PHP script in a HTML file?

Hmmmm...that's where a template comes in and you'd have to replace the `placeholder` with the value but as of this moment that's a bit too advance for you.

If you want to call PHP functions from a HTML file isn't not going to happen. You need a PHP file to process PHP.

Could you explain what you're trying to achieve in a bit more detail?

This post has been edited by codeprada: 17 June 2011 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,598
  • Joined: 08-August 08

Re: Passing form values from html to php function

Posted 17 June 2011 - 11:21 AM

I think the OP needs to memorize this list showing the (simplified) order in which things occur:
  • User takes some action.
  • Browser sends request to server. (User's machine)
  • Server runs PHP script. (Server is a different computer!)
  • Server sends HTML to browser.

Note that the browser never sees PHP. It only sees the HTML output by PHP.
Was This Post Helpful? 2
  • +
  • -

#14 mega01man  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: Passing form values from html to php function

Posted 18 June 2011 - 08:42 AM

View Postcodeprada, on 17 June 2011 - 10:38 AM, said:

PHP code is suppose to be in PHP files. Basically you want to place some values processed in the PHP script in a HTML file?

Hmmmm...that's where a template comes in and you'd have to replace the `placeholder` with the value but as of this moment that's a bit too advance for you.

If you want to call PHP functions from a HTML file isn't not going to happen. You need a PHP file to process PHP.

Could you explain what you're trying to achieve in a bit more detail?


Yes that is what I'm trying to achieve.
Do I still need to save the file as php to execute the php codes even though the server is setup to read php in html?
I've setup the apache conf to read the php code in html. That's why the last codes I posted worked. That's when I got the idea - how about just passing the values from the form to another html with the embedded php codes - first code: which passes the values to a php file, and second code: which accepts the return values from the php file.

View PostCTphpnwb, on 17 June 2011 - 11:21 AM, said:

I think the OP needs to memorize this list showing the (simplified) order in which things occur:
  • User takes some action.
  • Browser sends request to server. (User's machine)
  • Server runs PHP script. (Server is a different computer!)
  • Server sends HTML to browser.

Note that the browser never sees PHP. It only sees the HTML output by PHP.


I'll take note of this. Thank you very much! I noticed that also when I tried to view the source from the browser there's no php code in there.

Thank you very much for your replies and help! :)
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Passing form values from html to php function

Posted 18 June 2011 - 08:52 AM

View Postmega01man, on 18 June 2011 - 05:42 PM, said:

I've setup the apache conf to read the php code in html.

you are aware that this will put on an overhead on the server for all static (non-PHP containing) HTML files?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2