Welcome to WebmasterWorld Guest from 54.147.134.218

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to confirm before submitting the form value in database

     
1:14 pm on Jun 19, 2012 (gmt 0)

New User

joined:June 12, 2012
posts: 7
votes: 0


hi,
i am using registration form. if the user submit the form,it will show the form details which user have submitted ,
if he agree with that then user click the save button ..it will save in database.otherwise user will have edit button .
is this possible or not tell me?
i am new to this php and javascript...
thanks in advance ...
5:02 pm on June 30, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Sure, sounds reasonable enough. You can check to see if the user is already logged in and if so you will not show a registration form with a "save" option but rather you will display their current information which they can edit.
6:58 am on July 2, 2012 (gmt 0)

New User

joined:June 12, 2012
posts: 7
votes: 0


Thanks for replay..
Actually i want edit the form which consist of 20 fields.
Is there any easy way to do that?
Thanks advance...
11:57 am on July 3, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Easy? As in magical? ;)
You need to read in the data from the database and populate your HTML form with the data. Here is a pseudo code sample to give you an idea ...
<?php 
$userid = $_SESSION['userid'];
$sql = "SELECT firstname, lastname FROM table WHERE userid = {$userid}";
execute query, fetch row into variables
?>
<form>
First Name: <input name="firstname" value="<?php print $firstname; ?>">
Last Name: <input name="lastname" value="<?php print $lastname; ?>">
<input name="formSubmit" type="submit" value="Submit Changes">
</form>
3:58 pm on July 8, 2012 (gmt 0)

New User

joined:June 12, 2012
posts: 7
votes: 0


thanks
easy? i mean is there any way fetch the value from the database with out specific field ..
using array or something ..
because i need 20 fields values .
12:45 pm on July 9, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


If you have 20 fields you need to place them individually into your HTML so you are going to need to be specific.
3:44 pm on July 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


i mean is there any way fetch the value from the database with out specific field ..


Start with some variables . . .
$tablename = 'whaever_your_table_name_is';
$identifier = 'user_id';
$id = 12345; // whatever your user id is

Create some function to get field table names. You will need this in the get record function as well as in the event there are no values in post, such as on the first load of the page. There are probably more efficient ways, but off the top of my head . . . .

$fields = field_names($tablename);


function field_names($tablename) {
if (! isset($table)) { die("No table name in field names"); }
$fields = Array();
$counter=0;
$result = mysql_query("show columns from $table") or die ("Could not get table names");
while ($row = mysql_fetch_array($result)) {
$fields[$counter]=$row[0];
$counter++;
}
return $fields;
}


Now you have a list of fields. Create some function to get records from table X by unique identifier Y. This allows you to query ANY table for a record.


if (is_numeric($the_id) and ($the_id > 0)) {
$myvalues = get_record($tablename,$fields,$identifier,$id);
}
//
function get_record($table,$tablefields,$field,$target_id) {
//
$allvalues=array();
$select = "select * from $tablename where $field=$target_id";
$result=mysql_query("$select") or die("Could not get record for table $table");
//
if ($row=mysql_fetch_array($result)) {
foreach ($tablefields as $fld) { $allvalues[$fld] = $row[$fld]; }
}
return $allvalues;
}


Now you have all the values loaded (maybe! remember it will be empty on first load), squelch "undefined variable" and/or offset errors if there was no record found by setting the values to null only if there isn't an existing value.

foreach ($fields as $fld) { $myvalues[$fld]=(isset($myvalues[$fld]) and ! empty($myvalues[$fld]))?$myvalues[$fld]:null; }

Then you'd continue with your form like so.

<p><label for="fname">First Name:</label> <input type="text" name="fname" id="fname" value="<?php echo $myvalues['fname']; ?>"></p>

Or get slick about it; you'll have to make some if/else's to get this to work:

$ignoreme = ('id','member_status','access_level'); // whatever, fields you don't want in form


foreach ($fields as $fld) {
if (! in_array($fld,$ignoreme)) {
echo "<p><label for=\"$fld\">" . $english[$fld] . ":</label>
<input type=\"text\" name=\"$fld\" id=\"$fld\" value=\"" . $myvalues[$fld] . "\"></p>\n";
}
}


Entire form in five lines.

What is "$english?" An array you'd have somewhere with an association between field labels and actual field names. Along that thought, the previous "as posted" is a little dangerous as it reveals database field names in your forms, you should always create aliases somewhere internal to your programming (or, a table in the DB reserved for this purpose.) The previous is not copy and paste code (though I'm sure many will try) but it's to give you an idea of how to perform your tasks anonymously so they can be re-used and expandable.