/    Sign up×
Community /Pin to ProfileBookmark

Determining field type of $_POST variables

Hi Guys

Does anyone know if it’s possible for a script that is receiving $_POST variables to be able to determine what type of field has sent each variable?

i.e. the receiving script sees the incoming $_POST variables and can say: “this field is called ‘yes’, it contains ‘1’ [I]and it’s a checkbox[/I]. That field is called ‘name’, it contains ‘John’ [I]and it’s a textarea[/I]

Sorry – that’s the best way I can think of wording my question ? I hope it makes sense.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 05.2005 — Not that I know of. I'm pretty sure the HTTP data just sends name=data pairs.

I've never run into a situation where I needed to know, myself. If it was necessary, you could use some sort of naming convention for your form elements that would indicate this info.
Copy linkTweet thisAlerts:
@solomonauthorApr 05.2005 — mmm.. problem is I've writing a mailer script that (hopefully) anyone can use - just build a form with any field names and field types you like (add a couple of hidden fields) and send it to sendmail.php. sendmail.php will then bosh a quick e-mail together and off we go.

The tricky bit I want to develop it so when a user doesn't fill required fields (probably determined by more hidden fields) so sendmail.php will return back to the form and auto-fill the fields which have already been completed. Therefore (I'm guessing) sendmail.php will need to be able to determine what type of field sent the information so it will know how to auto-fill the form. It would be the most elegant solution - unless you can think of another way of going about it.
Copy linkTweet thisAlerts:
@NogDogApr 06.2005 — Hmmm...., you might be better off redirecting them back to the original form page:
[code=php]
<html>
<head>
<title>Form Error</title>
<META http-equiv="refresh" content="9;URL=<?php echo $_SERVER['HTTP_REFERER'] ?>">
</head>
<body>
<h1>Error</h1>
<p>Your form could not be processed because not all of the required fields were filled
in. You should be automatically redirected back to the form in a few seconds. If not,
<a href="<?php echo $_SERVER['HTTP_REFERER'] ?>">click here to return to the form</a>.
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@crh3675Apr 06.2005 — The problem is, if you have checkboxes, you would have to post the data for all of the checkboxes. What I would do is create a custom javascript submit that basically pulls the entire HTML content of the form into a hidden field onSubmit. So, when your script gets this hidden field with the form HTML, you can parse that accordingly.

<i>
</i>&lt;script type="text/javascript"&gt;

function form_custom_submit(f){
var hid=document.createElement("input");
hid.setAttribute("type","hidden");
hid.setAttribute("name","_formHTML");
hid.setAttribute("value",f.innerHTML);
f.appendChild(hid);
}

&lt;/script&gt;

&lt;form method="post" action="yourmailerscript.php" onsubmit="form_custom_submit(this)"&gt;

&lt;fields.....&gt;

&lt;/form&gt;
Copy linkTweet thisAlerts:
@solomonauthorApr 06.2005 — Do you mean that the sendmail script will receive a big hidden field that at contains the information required? So itherefore t'll just be a case of extracting it out of a large string?

One thing is, I would have preferred to do it server-side and not rely of js being enabled.
Copy linkTweet thisAlerts:
@solomonauthorApr 06.2005 — Ah, I see now! When you said: ... pulls the entire HTML content of the form into a hidden field onSubmit ...[/QUOTE]
I didn't quite realise you meant 'pulls the entire HTML content of the form into a hidden field onSubmit' :p

That's pretty powerful - that whole $_POST variable can be used to [I]completely[/I] rebuild the form from scratch without having send variables back to the original form and squeezing them in to the right places. The result is I just need to ask the users to pop in a little bit of javascript and change the form tag a touch...

hmm... I like ? thanks
Copy linkTweet thisAlerts:
@crh3675Apr 06.2005 — You could always do a "hidden" post-check. What I mean is to setup a hidden IFRAME and POSTthe form to the iframe. Let the IFRAME talk back to the main document. So, the main document doesn't have to reload. When the post-check passes, send back a value that will alloow the form to submit. I will try to get an example together for you
Copy linkTweet thisAlerts:
@crh3675Apr 06.2005 — <i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"
"http://www.w3.org/TR/REC-html40/frameset.dtd"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt; new document &lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--

function post_check(f){
f.target="postcheckframe";
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body lang="en"&gt;
&lt;form method="post" action="yourmailer.html" onsubmit="return post_check(this)"&gt;
Your name:&lt;br&gt;
&lt;input type="text" name="yourname" size="25"&gt;&lt;br&gt;
Your email:&lt;br&gt;
&lt;input type="text" name="youremail" size="25"&gt;&lt;br&gt;
&lt;input type="submit" value="Send" name="submit"&gt;
&lt;/form&gt;
&lt;iframe name="postcheckframe" id="postcheckframe" style="visibility:hidden"&gt;&lt;/iframe&gt;
&lt;/body&gt;
&lt;/html&gt;




Then, your mailer script would acces the parent document. I used an HTML just for example:
<i>
</i>&lt;script&gt;
parent.document.forms[0].yourname.value="I changed your name";
&lt;/script&gt;
×

Success!

Help @solomon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...