help how to make add form

General discussions related to php

Moderators: macek, egami, gesf

prinzzchyz
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Feb 15, 2013 3:38 pm

help how to make add form

Postby prinzzchyz » Fri Feb 15, 2013 3:51 pm

Hi

I am newbie in PHP
I wish someone who can assist me on my first project

my first project name is websticker and it is about inserting of information and it can also view in any browser

I have a database MySQL and i can connect without any errors

my first plan is how to make continue where end user can able to add new data without redirecting to other page.

i have two file which is insert.html and insert.php. In the insert.html when end user add new data and submit it will redirect to insert.php and prompt the 1 record added

what i want is, In the same page and message will prompt without redirecting and can also add new data.



for example:

insert.html

Code: Select all


<html>
<body>

<form action="insert.php" method="post">
ID: <input type="text" name="de_id">
Part Number: <input type="text" name="de_prtno">
Size by ": <input type="text" name="de_inch">
<input type="submit" name="submit">

</form>
</body>
</html>



for insert.php

Code: Select all

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("db_webstickerdemo", $con);


if(isset($_POST['submit'])) {

$sql="INSERT INTO tblde_sticker (de_id, de_prtno, de_inch)
VALUES
('$_POST[de_id]','$_POST[de_prtno]','$_POST[de_inch]')";

if (!mysql_query($sql,$con))
  die('Error: ' . mysql_error());

header('Location: insert.php');

}

echo "1 record added";

mysql_close($con);

?>



i hope some can understand my english.


Thanks

php9
New php-forum User
New php-forum User
Posts: 13
Joined: Fri Feb 22, 2013 2:20 am

Re: help how to make add form

Postby php9 » Fri Feb 22, 2013 2:24 am

An option is to keep the user on the same page. So your form is shown by insert.php and it also submits to insert.php. By check if the form is submitted you can do the insert, show a message and let the page show the form as it would do if there was no submission (ie first page view).

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: help how to make add form

Postby seandisanti » Wed Mar 13, 2013 8:34 am

php9 wrote:An option is to keep the user on the same page. So your form is shown by insert.php and it also submits to insert.php. By check if the form is submitted you can do the insert, show a message and let the page show the form as it would do if there was no submission (ie first page view).

yes, you can tell if you're responding to a form by just verifying $_POST...

Code: Select all

if ($_POST) { //responding to form
...
} else { //display form
...
}

but technically even though it's redirecting to itself, the visitor is still leaving the page. If you wanted to really keep the user on the same page, you would have to use something like ajax to interact with the database. w3schools has a lot of primers and lessons on ajax at http://www.w3schools.com/ajax/default.asp that should be able to get you going in no time.


Return to “PHP General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron