?
Solved

Form processing in PHP

Posted on 2004-11-03
15
Medium Priority
?
291 Views
Last Modified: 2006-11-17
Hi,
Wonder if anyone can help. I've got the following code, and have a problem with the code that processes the form. See below (point 3) for details of the problem.


1. FORM:

</html>
<body>
<form action="processform.php" method="post">

<br /><br />
<input type="text" name="/ContentItem[1]/CommonElements[1]/Name[1]" maxlength="255" value="First new asset"/>
<br /><br />

Text Area : <textarea name="news_title" rows="2" cols="80">Some content in a text area</textarea>
<br /><br />
<input type="submit" name="submit" value="submit">&nbsp;
<br /><br />
</form>

</body>
</html>



2. Code to process the form, and display the form variables: processform.php
<?php

            $FormVariables=$_POST;
    foreach ($FormVariables as $Name=>$Value) {
            echo "<br /><br />Name : ". $Name . "<br />";

      if      (gettype($Value) == "array") {
        $ArrayComponent = "";
        foreach ($Value as $ArrayElement) {
          $ArrayComponents .= "$ArrayElement, ";
        } # End of foreach ($Value as $ArrayElement)
        $Value = substr($ArrayComponents,0,strlen($ArrayComponents)-2);
      }

            echo "Values: $Value<br />";
    } # End of foreach ($FormVariables as $Name=>$Value)
?>


3. So, the output from this for the first form element is:

Name : /ContentItem
Values: First new asset

BUT - the first form elements' name actually is:
"/ContentItem[1]/CommonElements[1]/Name[1]"

Anyone any ideas how I can get PHP to properly trap and/or display this field name?
The field name is this format since it is an XPATH.

Any thoughts welcome. Do please try the above code to replicate the above problem.
Thanks,
Ben.















0
Comment
Question by:bcops
  • 7
  • 4
  • 2
  • +1
15 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12485671
You can't use [ and ] in a name otherwise it regards it as a html array, if you use print_r($_POST); you will see this.

So two work arounds you might consider, either switch the value and name in the inputs and rework your code logic a little, eg:

<input type="text" name="First new asset" maxlength="255" value="/ContentItem[1]/CommonElements[1]/Name[1]"/>

OR

instead of square brackets use round brackets and str_replace them later, eg:

<input type="text" name="/ContentItem(1)/CommonElements(1)/Name(1)" maxlength="255" value="First new asset"/>

then relace:

 echo "<br /><br />Name : ". $Name . "<br />";

with:

 $Name = str_replace("(","[",$Name);
 $Name = str_replace(")","]",$Name);
 echo "<br /><br />Name : ". $Name . "<br />";
0
 

Author Comment

by:bcops
ID: 12487990
Hi Diablo84,

Thanks. Funnily enough I had thought the same about the round and square brackets, but wondered if there was any other way.
I'll see if I can make this work with the rest of the logic, and see if anyone else replies.

Thanks, Ben.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 2000 total points
ID: 12488139
No problem Ben, the problem you will face is that this is a HTML issue rather then a PHP issue, and one that to the best of my knowledge is beyond your control.

The presence of [and ] in a form elements means it will be regarded as an array, generally speaking form elements should follow the same naming scheme that variables use to avioid any problems (that is a letter or an underscore followed by any number of letters, underscores or numbers).

If you set up a simple example you will see how the brackets are translated into an array, eg:

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="/ContentItem[1]/CommonElements[1]/Name[1]" maxlength="255" value="First new asset"/>
<input type="submit" name="submit" value="submit">
</form>

Everything after the first brackets will be disregarded and it will be read as an array named ContentItem with the value "First new asset" stored in the index 1.

Best of luck with your code.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:_the_mars_
ID: 12504838
You can encode the strings with special chars with urlencode. You must do it twice, because it is decoded once already by the browser or PHP (I don't know). Therefore decoding once is enough.

Your input line becomes:

    <input type="text" name="<?php echo urlencode(urlencode('/ContentItem(1)/CommonElements(1)/Name(1)')); ?>" maxlength="255" value="First new asset"/>

And change this line in your PHP code:
    echo "<br /><br />Name : ". urldecode($Name) . "<br />";


Nice to see you follow XHTML guidelines!
0
 

Author Comment

by:bcops
ID: 12504888
Hi _the_mars_,

Thanks for this. I'll definitely give this a whirl. Many thanks,
Ben.

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12504915
You dont need to encode it twice,

<input type="text" name="<?php echo urlencode("/ContentItem[1]/CommonElements[1]/Name[1]"); ?>" maxlength="255" value="First new asset"/>

Nice idea though.

If you was referencing the post array any other way i may steer clear from this as referencing the specific key in the array would be tricky however as you are looping through the array its not so inconvienient.
0
 

Author Comment

by:bcops
ID: 12504982
Thanks to both - I'll let you know whether it works in current logic, when I get a moment to try it out.

I'll also confirm the once/twice encoding thing

Thanks again,
Ben.
0
 
LVL 2

Expert Comment

by:_the_mars_
ID: 12514414
Yes Diablo84, you are right.
Encoding twice is not needed to do yourself.
However it is encoded twice (you can see this if sending your form with method GET), but your browser does encoding once before sending and the webserver does decoding once after receiving.

Therefore Ben encoding once is enough.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12682222
Hi Ben,

did you get it working in the end?
0
 

Author Comment

by:bcops
ID: 12685646
Hi,

To be honest - I've not had time to try this yet - when I do I'll respond and award points. Most definitely.
Thanks, Ben.

0
 

Author Comment

by:bcops
ID: 12834752
Sorry not awarded points yet, not had time to look at this. When I do, and if it works - I'll award the pts
Ben
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12981243
Any update here?

Venabili
PHP Cleanup Volunteer
0
 

Author Comment

by:bcops
ID: 12981296
Not yet - but there will be. So please leave this question open. This will get answered and points will be awarded.
Thanks, Ben.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 13101048
Any update?
0
 

Author Comment

by:bcops
ID: 13101328
Not yet, but please leave this - and most of my other questions open. I will answer them, alot of my questions are R&D related that do get looked at when time is available. But please do leave this open, I will answer it and will award points.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question