Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code break-down 1

Status
Not open for further replies.

cian

Technical User
Oct 11, 2001
1,383
Hi guys,

i'm working on my first script (form processing script), reworking anyother script which I have.
I have one piece which I know what it does but I have no idea how to take it apart.
What I want to do is
1. understand what the heck is happening
2. separate the code into 2 seperate parts

The piece of code i'm stuck on is:

if ($required) {
$ra=explode(",", $required);
$num=count($ra); }
$results="";
reset ($HTTP_POST_VARS);
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
if (($key!="sendto") && ($key!="reply_from") && ($key!="reply_fromaddr") && ($key!="reply_subject") && ($key!="reply_message") && ($key!="subject") && ($key!="required") && ($key!="redirect")) {
for($i=0;$i<$num;$i++) {
if (($key==$ra[$i]) && ($val==&quot;&quot;)) DIE (&quot;<html><script language='JavaScript'>alert('Please fill in the $ra[$i] field!'),history.go(-1)</script></html>&quot;); }
$results.=&quot;$key: $val\n&quot;; }}

i THINK it does the followig
- check what form fields are required and alerts the user to fill them in
- check's what form fields are defined in the script and submits any other form fields as $results

Can you help me break this code down?

The problem i'm stuck on in the script is making it generic so that all form fields are submitted, BUT some fields will be defined in the script so I can't have those repeated.

Think you can help?? :)



É ::
 
The script snippet examines the variable $required for form fields which must be filled out. $required may contain a comma-delimeted list of fields, such as &quot;field1,field2,field3&quot;

The script then looks at all submitted form fields. If the field's name is in $required, and if the field's name is not one of sendto, reply_from, reply_fromaddr, reply_subject, reply_message, subject, required, or redirect, and if the field is empty, it generates an error it dies. Otherwise, it just outputs the field name and value.

I found it easier to examine the script by reformatting its layout like:
Code:
if ($required)
{
	$ra=explode(&quot;,&quot;, $required);
	$num=count($ra);
}

$results=&quot;&quot;;

reset ($HTTP_POST_VARS);

while (list ($key, $val) = each ($HTTP_POST_VARS))
{
	if (	($key != &quot;sendto&quot;) &&
			($key != &quot;reply_from&quot;) &&
			($key != &quot;reply_fromaddr&quot;) &&
			($key != &quot;reply_subject&quot;) &&
			($key != &quot;reply_message&quot;) &&
			($key != &quot;subject&quot;) &&
			($key != &quot;required&quot;) &&
			($key != &quot;redirect&quot;)
		)
	{
		for($i=0; $i<$num; $i++)
		{
			if (
					($key==$ra[$i]) &&
					($val==&quot;&quot;)
				)
			{
				DIE (&quot;<html><script language='JavaScript'>alert('Please fill in the $ra[$i] field!'),history.go(-1)</script></html>&quot;);
			}
		}

		$results.=&quot;$key:  $val\n&quot;;
	}
}
Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks sleipnir214,
that's a lot clearer now. Formatting helps too :)

Can you advise me on how I could divide the script. Ideally i'd like to create the script to check if required fields have been filled and also separatly to read all form variables (to be generic) but to display the specified ones as well as the unspecified ones. Does that make sense??

All I could find in the manual is the following which processes all form variables.
foreach ($_POST as $var => $value) {
echo &quot;$var = $value<br>\n&quot;;
}
so I have to figure out how to match this in there somewhere.. who said php was easy?


É ::
 
Typical newbie, pick a difficult one for a first project.

What i'm basically trying to do is to take the original code I posted above and to firstl check that required fields have been filled, and secondly process the form which must be generic so the use can specify whatever form fields (s)he wants.

The original script does that but I want to separate the code into separate blocks.

Should I start with something simpler? :)


É ::
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top