Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Self-contained form?

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
0
0
US
Hello everyone,

I'm having trouble putting together a php form that inserts a new record into a database but is self-contained (i.e. the action clause in the form tag does not go to another php file). I've found a couple examples online but are imperfect because they generate parsing errors when I copy and paste them in my computer to test them out. I'm running Postgresql as my database. Can anyone show me how to do this or point me to a tutorial that actually works?

Thanks,
Jisoo22
 
Hi,

The way you will want to do this is have the form post to itself, meaning form.php posts to form.php and you will have to setup variables to see if the form is filled out and then do your record insertion if the set credentails are correct. If the values are not set and it appears that the page has not been posted to then simply show the form and don't do the record insertion.

Does that make sense?

Hope this helps!

relax.gif


Keep up to date with my 2003 NHL Playoffs Wallpaper
 
I think I see what you're saying but I'm not quite sure exactly how to go about it. I've pasted my lastest attempt below as it still doesn't work. Can anyone tell me what's wrong with it?

Thanks,
Jisoo22

<html>
<head><basefont face=&quot;Arial&quot;></head>
<body>
<h2>Information Page</h2>

<?
// form not yet submitted
// display form
if (!$submit)
{
?>
<form action=&quot;<? echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;POST&quot;>
Last Name:<br>
<input name=&quot;name_last&quot; type=&quot;text&quot; size=&quot;50&quot;>
<p>
First Name:<br>
<textarea name=&quot;name_first&quot; rows=&quot;6&quot; cols=&quot;40&quot;></textarea>
<p>
User Email:<br>
<input name=&quot;user_email&quot; type=&quot;text&quot; size=&quot;10&quot;>
<p>
User Position:<br>
<input name=&quot;user_position&quot; type=&quot;text&quot; size=&quot;30&quot;>
<p>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Add&quot;>
</form>
<?
}
else
{
// form submitted
// prepare to insert data

// database access parameters
// alter this as per your configuration
$db_name = &quot;test2&quot;;
$table_name = &quot;users&quot;;

// open a connection to the database server
$connection = pg_connect(&quot;dbname=$db_name port=5432&quot;);

if (!$connection)
{
die(&quot;Could not open connection to database server&quot;);
}

// error checks on form submission go here

// generate and execute a query
$query = &quot;INSERT INTO $table_name VALUES
(nextval('user_id'), 'name_last', 'name_first', 'user_email', 'user_position')&quot;;
$result = pg_query($connection, $query) or die(&quot;Error in query:
$query. &quot; . pg_last_error($connection));

echo &quot;Data successfully added.&quot;;

// close database connection
pg_close($connection);
}
?>
</body>
</html>
 
Let me put it my own way hope u get the idea.

<?php
function form(){
the form html code goes here
with 1 hidden field.
<input type=&quot;hidden&quot; name=&quot;op&quot; value=&quot;process&quot; />
}
// end form()

function process($1, $2, $3){
the processing code here
}

if(!($HTTP_POST_VARS['op'])){
$op = &quot;form&quot;;
}else{
$op = $HTTP_POST_VARS['op'];
}

switch($op){
case form:
form() //call the function. Just show the form.
break;

case process:
process($HTTP_POST_VARS['2'], $HTTP_POST_VARS['2'], $HTTP_POST_VARS['2']);
// Call the process() function with arguments to be process.
break;
}

Use $HTTP_POST_VARS is safer since older version of php will not understand the new $_POST.

This is not the best but this how I do it.
 
Hi,

I would suggest that you add some good validation into this too. Make sure that all fields are filled out correctly, b4 processing the info to the db. Once you have determined how the page has been called (either posted to by filling out the form, or first time arrival) then you continue the actions. If the form has been posted to then you will want to start the form validation, if the form has not been posted to then chances are that it's the first time arrival and you will just want to show the form.

I would suggest you do a couple extra things. You can add a hidden field to the form to pass a variable to see if the page has been posted to from itself and to check the referrer. If referrer is itself and the hidden variable is there then you know that you need to continue with your validation and then do the record insertion. If not then just show the form.

Does that make sence to you?

Hoep this helps!

relax.gif


Keep up to date with my 2003 NHL Playoffs Wallpaper
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top