Form processing in PHP

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.















bcopsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
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
bcopsAuthor Commented:
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
Diablo84Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

_the_mars_Commented:
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
bcopsAuthor Commented:
Hi _the_mars_,

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

0
Diablo84Commented:
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
bcopsAuthor Commented:
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
_the_mars_Commented:
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
Diablo84Commented:
Hi Ben,

did you get it working in the end?
0
bcopsAuthor Commented:
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
bcopsAuthor Commented:
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
VenabiliCommented:
Any update here?

Venabili
PHP Cleanup Volunteer
0
bcopsAuthor Commented:
Not yet - but there will be. So please leave this question open. This will get answered and points will be awarded.
Thanks, Ben.
0
VenabiliCommented:
Any update?
0
bcopsAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.