View Full Version : Combine 2 .php web forms into 1 .php web form.

05-02-2016, 03:38 PM
Thank any one for viewing! Up front, Im 6 weeks into .php, therefore please be easy one me. I have 2 php forms. Form#1 collects user data / saves to sql db. Form#2 edits db records / saves to db. I also us a Form#3 that selects db records by keyfield and repopulates both Form#1 and Form#2.

How is it possible to combine Form#1 and Form#2 into a single form. Where this single form handles both the user data collection / save and edit / save.
Any direction, sample code, instruction or thoughts would be so helpful. I have read and tested for a solution with all failures. Please help for a solution. Thank You!


<!-- // dB Connection - php save form input data to Database // -->

if (isset($_GET['name'])){

$tapcon_title = $_GET['tapcon_title'];

$name = $_GET['name'];

$age = $_GET['age'];

$hobby = $_GET['hobby'];

$background_image = $_GET['background_image'];

$trn_date = date("Y-m-d H:i:s");

$query = "INSERT into `search` (tapcon_title, name, age, hobby, background_image, trn_date) VALUES ('$tapcon_title', '$name', '$age', '$hobby', '$background_image', '$trn_date')";
$result = mysql_query($query);

echo "<div class='form'><h1>Save to db was successfull.</h1><p> I need a button here to close this page, revieling the TapCon Builder page with form input attrubutes in place.</p>

<br/><h3>Click here to return <a href='https://jakursmu.com/tapcon_builder_development/tb_form_insert_to_db.php'>Db Search</a></h3>



// Search db for record - display result in form inputs
// Form1.php


$data = 'SELECT * FROM `search` WHERE `name` = "'.$search.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$data2 = mysql_fetch_array($query);


<!-- // Form.html - get key detail of record from database // -->

<div class="form">

<h2> Search db for earlier TapCon by Name</h2>

<form target="_self" name="form" method="GET" action="">

Enter TapCon Name <input type="text" name="search" /> <br /><br />

<input type="submit" value="Search db for TapCon" />


<!-- // TapCon Builder Form #1 Inputs // -->

<div class="form">

<h2> TapCon Builder</h2>

<form target="_blank" method="GET" action="">

<p> TapCon Title:<input name="tapcon_title" type="text" value="<?php echo $data2[tapcon_title]?>" /> </p>

<p> Name:<input name="name" type="text" placeholder="Name" value="<?php echo $data2[name]?>"/> </p>

<p> Age:<input name="age" type="text" placeholder="Age" value="<?php echo $data2[age]?>"/> </p>

<p> Hobby:<input name="hobby" type="text" placeholder="Hobby" value="<?php echo $data2[hobby]?>" /> </p>

<p> TapCon Background Image / Color:<input name="background_image" type="file" value="<?php echo $data2[background_image]?>"/></p>

<input type="submit" value="Preview TapCon" formaction="tapcon_builder_template.php" >

<input type="submit" value="Save TapCon to DB" formaction="" >



<!-- // Form1.php - Display / Edit record from database Form #2 Inputs // -->

<div class="form">

<h2> TapCon Editor</h2>

<form target="_self" name="form" method="GET" action="">

<p>TapCon Title:<input type="text" name="tapcon_titlefield" value="<?php echo $data2[tapcon_title]?>" /></p>

<p>Name:<input type="text" name="namefield" value="<?php echo $data2[name]?>"/></p>

<p>age:<input type="text" name="agefield" value="<?php echo $data2[age]?>"/></p>

<p>hobby:<input type="text" name="hobbyfield" value="<?php echo $data2[hobby]?>"/></p>

<p> TapCon / <input name="background_image" type="file" value="<?php echo $data2[background_image]?>"/></p>

<p><input type="hidden" name="keyfield" value="<?php echo $search?>"></p>
<p><input type="submit" value="submit to Edit TapCon">


<h2> TapCon Edit Results</h2>

<!-- // Form2.php Db Edit Results // -->

$data = "UPDATE `search` SET tapcon_title='$Tapcon_Title', name='$Name', age='$Age', hobby='$Hobby', background_image='$Background_Image' WHERE name=".'"'.$Key.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());


<!-- display the changed record from database -->

TapCon Title: <?php echo $Tapcon_Title?><br>
Name: <?php echo $Name?><br>
Age: <?php echo $Age?> <br>
Hobby: <?php echo $Hobby?><br>
Background Image: <?php echo $Background_Image?><br>


<br />

<?php } ?>


05-02-2016, 05:55 PM
You can make a single form be for data entry or editing existing data by using a hidden field that holds the id of the database recored when editing data. In the form processing code, if the hidden id field is empty, you would INSERT new data. If the hidden form field has a valid id in it (and the currently logged in user is authorized to edit the record), you would UPDATE an existing record using that id.

There's a couple of things your code needs to do -

1) The form should be method='post'. This is both a convention (you are altering data on the server and should be posting the data to the server) and to prevent things like search engines from submitting blank data to your form processing code (search engines will only make get requests, not post requests).

2) You need some security to control if, at all, and who can access the form and submit new data or edit existing data. If you have a currently logged in user, they should be able to edit their own data and both the form and form processing code should be checking that the requested search term you are using to specify the record to edit matches the logged in user. Likewise for inserting new data. Who should be able to insert new data, a logged in user who doesn't already have a record, or an admin on the site?

05-02-2016, 06:52 PM

Thank You for reading and responding to my needs. Let me respond back to you. I understand that POST should be used when adding and editing data to the server. I am using GET only for the reason, POST will NOT post any new or edited data to server. I randomly tryed GET, then new and edit data was successfully entered into sql data base.

I would like to understand how 1 web form will handle both Add / Edit / Save to the database. Then, I will need to learn why GET will allow Add / Edit / Save to db and POST will NOT allow this.

Would you have any knowledge to help me to combine the 2 forms?

Thank You for considering.