Accessing Form Elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deerman
Forum Newbie
Posts: 5
Joined: Sat Aug 04, 2007 11:59 am

Accessing Form Elements

Post by Deerman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I want to access form elements (text boxes) in HTML via php. Can this be done.

Thank you

Code: Select all

<html>

<head>
<title>New Page 1</title>
</head>

<?php


// Want to access the form elements from here.


?>

<body>

<form name="puzzle">
	<p align="center"><font size="5">Sudoku</font><font size="5"> Solver<br>
	</font><br>
	<input type="text" name="T1" size="1">&nbsp;
	<input type="text" name="T2" size="1">&nbsp;
	<input type="text" name="T3" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T5" size="1">&nbsp;&nbsp;
	<input type="text" name="T6" size="1">&nbsp;&nbsp;
	<input type="text" name="T7" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T8" size="1">&nbsp;&nbsp;
	<input type="text" name="T9" size="1">&nbsp;&nbsp;
	<input type="text" name="T10" size="1"><br>
	<input type="text" name="T11" size="1">&nbsp;
	<input type="text" name="T12" size="1">&nbsp;
	<input type="text" name="T13" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T14" size="1">&nbsp;&nbsp;
	<input type="text" name="T15" size="1">&nbsp;&nbsp;
	<input type="text" name="T16" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T17" size="1">&nbsp;&nbsp;
	<input type="text" name="T18" size="1">&nbsp;&nbsp;
	<input type="text" name="T19" size="1"><br>
	<input type="text" name="T23" size="1">&nbsp;
	<input type="text" name="T24" size="1">&nbsp;
	<input type="text" name="T25" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T27" size="1">&nbsp;&nbsp;
	<input type="text" name="T28" size="1">&nbsp;&nbsp;
	<input type="text" name="T29" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T31" size="1">&nbsp;&nbsp;
	<input type="text" name="T32" size="1">&nbsp;&nbsp;
	<input type="text" name="T33" size="1"></p>
	<p align="center"><input type="text" name="T34" size="1">&nbsp;
	<input type="text" name="T35" size="1">&nbsp;
	<input type="text" name="T36" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T38" size="1">&nbsp;&nbsp;
	<input type="text" name="T39" size="1">&nbsp;&nbsp;
	<input type="text" name="T40" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T42" size="1">&nbsp;&nbsp;
	<input type="text" name="T43" size="1">&nbsp;&nbsp;
	<input type="text" name="T44" size="1"><br>
	<input type="text" name="T45" size="1">&nbsp;
	<input type="text" name="T46" size="1">&nbsp;
	<input type="text" name="T47" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T49" size="1">&nbsp;&nbsp;
	<input type="text" name="T50" size="1">&nbsp;&nbsp;
	<input type="text" name="T51" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T53" size="1">&nbsp;&nbsp;
	<input type="text" name="T54" size="1">&nbsp;&nbsp;
	<input type="text" name="T55" size="1"><br>
	<input type="text" name="T56" size="1">&nbsp;
	<input type="text" name="T57" size="1">&nbsp;
	<input type="text" name="T58" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T60" size="1">&nbsp;&nbsp;
	<input type="text" name="T61" size="1">&nbsp;&nbsp;
	<input type="text" name="T62" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T64" size="1">&nbsp;&nbsp;
	<input type="text" name="T65" size="1">&nbsp;&nbsp;
	<input type="text" name="T66" size="1"></p>
	<p align="center"><input type="text" name="T67" size="1">&nbsp;
	<input type="text" name="T68" size="1">&nbsp;
	<input type="text" name="T69" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T71" size="1">&nbsp;&nbsp;
	<input type="text" name="T72" size="1">&nbsp;&nbsp;
	<input type="text" name="T73" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T75" size="1">&nbsp;&nbsp;
	<input type="text" name="T76" size="1">&nbsp;&nbsp;
	<input type="text" name="T77" size="1"><br>
	<input type="text" name="T78" size="1">&nbsp;
	<input type="text" name="T79" size="1">&nbsp;
	<input type="text" name="T80" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T82" size="1">&nbsp;&nbsp;
	<input type="text" name="T83" size="1">&nbsp;&nbsp;
	<input type="text" name="T84" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T86" size="1">&nbsp;&nbsp;
	<input type="text" name="T87" size="1">&nbsp;&nbsp;
	<input type="text" name="T88" size="1"><br>
	<input type="text" name="T89" size="1">&nbsp;
	<input type="text" name="T90" size="1">&nbsp;
	<input type="text" name="T91" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T93" size="1">&nbsp;&nbsp;
	<input type="text" name="T94" size="1">&nbsp;&nbsp;
	<input type="text" name="T95" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="T97" size="1">&nbsp;&nbsp;
	<input type="text" name="T98" size="1">&nbsp;&nbsp;
	<input type="text" name="T99" size="1"></p>
	<p align="center"><input type="button" name="button" value="   Solve   ">
	
</form>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

You need to POST your data first if you wish to input the textbox values into a database or similar.

Code: Select all

$t1 = $_POST['t1'];

If you want to extract information out of a databse or similar, and put it into your textboxes, you would do the following:

Code: Select all

<input type="text" value="<?php echo $t1; ?>" name="T1" size="1">

If you want to change cetain aspects of the element, eg, height, value etc, without refreshing the page you will need to use Javascript.

Code: Select all

var t1 = document.getElementById('t1');
t1.height = 'whatever';
Last edited by iknownothing on Sat Aug 04, 2007 12:56 pm, edited 1 time in total.
Deerman
Forum Newbie
Posts: 5
Joined: Sat Aug 04, 2007 11:59 am

RE:

Post by Deerman »

There is no data to "POST"

This code produces a 9X9 Sudoku grid. The user will fill in some numbers into the HTML text fields and I want php to be able to read all of the text fields to see which ones have a number in them.

Thanks.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

If you want to use PHP, then you will, infact need to POST your data first.

It looks to me as though you would rather use Javascript.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: RE:

Post by superdezign »

Deerman wrote:There is no data to "POST"

This code produces a 9X9 Sudoku grid. The user will fill in some numbers into the HTML text fields and I want php to be able to read all of the text fields to see which ones have a number in them.

Thanks.
So? When they are done, don't they have to click a button that tells you so? Then, you get all of the POSTED data and process it to see if they completed it validly or not.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

C'mon guyz, he did not specified any form method and the default is GET. $_REQUEST is universal.
Deerman
Forum Newbie
Posts: 5
Joined: Sat Aug 04, 2007 11:59 am

RE:

Post by Deerman »

Hey everybody, thanks for the answers. I'm a complete beginner in PHP. That's why I'm finding difficulty with this. Could anybody post a short example (using code) that does what I asked. I just need an example to work off of.

Thank you.
Deerman
Forum Newbie
Posts: 5
Joined: Sat Aug 04, 2007 11:59 am

RE:

Post by Deerman »

I look at a tutorial... typed this code in:

Code: Select all

import_request_variables("gp", "formval_");

echo "Ship = ".$formval_T1;
echo "<br />";
but still no go.

It says that T1 is undefined.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Answer this question: Do you want the grid to be a form-field-per-box setup that, when the user enters a number between 1 and 9, that nu,ber automatically get fetched and used by the application? Or are you expecting the user to complete the grid and hit a button?
Deerman
Forum Newbie
Posts: 5
Joined: Sat Aug 04, 2007 11:59 am

Post by Deerman »

I'm expecting the user to enter all of the data then press a button.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Deerman wrote:I'm expecting the user to enter all of the data then press a button.
Then once the form is submitted, all of the form data will be in the $_POST superglobal array.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

If you want the form to be submitted to it self, then use

Code: Select all

<form method=\"post\" action=\"<?PHP $_SERVER[PHP_SELF]\ ?>">
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Some of the members here have shown me some days ago, that using $_SERVER[PHP_SELF] is vulnerable to XSS attacks. You should use empty action intstead - that means the action is the current page.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

That is true. PHPSELF is not good, but if the script is only going to be used by your self then it's ok.......
Post Reply