Can a form be written in this way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Can a form be written in this way

Post by dude81 »

Don't know which is right place for this post.
I've use case where I should not use make more than one php file. I've to make add,edit,delete along with paginantion.
I've made a one single form in which seperate form fields for edit, sepearate form fields for addition. and two similar buttons for calling same javascript source file using same javasctript function, Two submit buttons. The add and edit are seperated with if logic, so there is no harm from my side as I thought.

what will be the pros and cons of using such forms.

Im pasting here a sample code of that

Code: Select all

<form>
<?php
if($action=='edit'){
echo  "<input type=hidden name=id[0] value='$ID'>".
              "<table align=left width='90%' cellspacing='0' cellpadding='0' border='1'><tr><td><b>Edit</b></td></tr>\r\n". "<tr><td><h3>Name</h3></td> <td><input type=hidden name=action value='edit'><input type=text name=name value=".$result_obj->name."></td></tr>". "<tr><td><h3>Periodcity</h3></td> <td><select name=period>".periodicity($result_obj->period)."</select></td></tr>". "<tr><td><h3>Category</h3></td><td><select name=cat_ID>".$cats."</select></td></tr>". "<tr><td><h3>Location</h3></td><td><input type=text name='location' value='".htmlentities($location)."'></td><td align=left valign='top'><p class='submit'><input type='button' value='Show' onclick=testDef(location.value)></p><strong>Search Results=</strong><div id='es'></div><strong> Details</strong><div id='asa'></div></td></tr>"."<tr><td><h3>Url</h3></td><td><input type=text name=source size=65 value=".$result_obj->source."></td></tr>"."<tr><td>&nbsp;</td><td align=right><p class='submit'><input type='submit' name='add' value='Save' /></p></td></tr>"."</table>";
}else{

 echo  "<input type=hidden name=d[0] value='0'>".
                   "<table align=left width='90%' cellspacing='0' cellpadding='0' border='1'><tr><td><b>Add New</b></td></tr>\r\n". "<tr><td><h3>Name</h3></td> <td><imput type=hidden name=action value='add'><input type=text name=name value=''></td></tr>".
		   "<tr><td><h3>Periodcity</h3></td> <td><select name=period>".periodicity()."</select></td></tr>".
		   "<tr><td><h3>Category</h3></td><td><select name=cat_ID[0] >".$cats."</select></td></tr>".
		   "<tr><td><h3>Location</h3></td><td><input type=text name='location' value=''></td><td align=left valign='top'><p class='submit'><input type='button' value='Show' onclick=testDef(location.value)></p><strong>Search Results=</strong><div id='es'></div><strong>Details</strong><div id='asa'></div></td></tr>". "<tr><td><h3> Url</h3></td><td><input type=text name=source[0] size=65 value='""'></td></tr>"."<tr><td>&nbsp;</td><td align=right><p class='submit'><input type='submit' name='add' value='Save' /></p></td></tr>"."</table>";
}?>
</form>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't see why you couldn't get it in the same form. To edit values, you just do something like this..

Code: Select all

<?php
echo '<input type="text" name="name_first" size="20" value="'.htmlentities($name_first, ENT_QUOTES).'" />';
?>
To add values, you could switch the action of the form.. or have the processing script detect what you are doing with a series of if()'s.. but I don't think you need to have if()'s to display the form.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Yes, the way you said is the general way which many people implement and I myself implemented the same earlier.
I see the following disadvantages improperly optimized code.
Any more points??
Post Reply