Jump to content

[SOLVED] PHP FORM-add submit button for insert records to database-HOW TO DO THIS?


Recommended Posts

Hi guys i need help with my form. First file post.php take record and send it to post2.php. Now records are insert into databse without submit button press and that is my problem.

I need working submit button. When user click on submit button=insert data to database. Sorry for my english. Thanks George


<html>
<form action="post2.php" method="post">
<form>
<ul>Name: <input type="text" name="name" /></ul>
<ul>Surname: <input type="text" name="surname" /></ul>
<input type="submit" />
</form>
</html>

<html>
<head>
<style type="text/css" media="screen">@import "css/style2.css";</style>
</head>
<body>
<?php 
/* insert record check */
if (strlen($name) <2)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>");
}
if (strlen($name) >10)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>");
}

if (strlen($name) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>");
}
if (strlen($surname) <2)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>");
}
if (strlen($surname) >10)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>");
}

if (strlen($surname) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>");
}
?>

<H2>You write:</H2>
<strong>Name</strong> <?php echo $_POST["name"]; ?><br/>
<strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/>
[color=yellow]<input type="submit" value="submit"/>[/color]

<?php
/*this is my problem */

if($susbmit) {
/*connection to DB */
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("formular", $con);
$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST[name]','$_POST[surname]')";

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

/*this belong to if($submit) */
  }
echo "<br>Your record was added.";

mysql_close($con)

}
?>
</body>
</html>

Try

 

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

 

And ensure your form has

 

<input type="submit" name="submit" value="submit">

 

Name: Name of the submit button in php ($_POST['submit'])

Value: What to display on the submit button.  Also the *value* of $_POST['submit']

Type: This says it's a submit button, and not something else

 

Note that if you use javascript's submit(), you may have trouble with naming your submit button "submit" as well.

Thanks for fast reply! So i done what you told me. But there is a trouble: Error: Query was empty. Php don't see inserted records from post.php.  Why?

Here is my updated code that causes error:

only change end form post.php:
<strong>name</strong> <?php echo $_POST["name"]; ?><br/>
<strong>surname</strong> <?php echo $_POST["surname"]; ?><br/>
<input type="submit" name="submit" value="submit">

 

post2.php

 

<?php
/*connect to DB  */
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
if ($_POST['submit'] === 'submit') {
mysql_select_db("formular", $con);
$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST[name]','$_POST[surname]')";
}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<br>.";

mysql_close($con)


?>

<html>

<form action="post2.php" method="post">

<form>

<ul>Name: <input type="text" name="name" /></ul>

<ul>Surname: <input type="text" name="surname" /></ul>

<input type="button" onclick"javascript:this.form.submit();" />

</form>

</html>

<html>

<form action="post2.php" method="post" name="frm">

<form>

<ul>Name: <input type="text" name="name" /></ul>

<ul>Surname: <input type="text" name="surname" /></ul>

<input type="button" onclick"javascript:document.frm.submit();" />

</form>

</html>

You misunderstood me.

What you post,it already works. That is not trouble.

My trouble is: that record are insert to database withnout user confirmation (submit button)

 

I have 2 files

-------------------

1. post.php with FORM (this one take information from user and show it with post2.php)

2. post2.php (show records from user whitch user add in post.php and have to insert these records to database BUT AFTER user CLICK ON SUBMIT BUTTON-this do not work, data are insert without submit button click)

 

everithing working fine but data are insert to database withnout user confirmation (submit button), so i want just this.

this is code of post2.php

<html>
<head>
<style type="text/css" media="screen">@import "css/style2.css";</style>
</head>
<body>
<H2>You wrote:</H2>
<strong>Name</strong> <?php echo $_POST["name"]; ?><br/>
<strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/>
<input type="submit" name="submit" value="submit">
<?
/*connection to database */
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("formular", $con);
if ($_POST['submit'] === 'submit') {
$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST[name]','$_POST[surname]')";
}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<br>Record was inserted.";

mysql_close($con)


?>
</body>
</html>

Thanks everynone for help

change this

$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST[name]','$_POST[surname]')";

 

to this

 

$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST['name']','$_POST['surname']')";

if (strlen($surname) >10)

{

die("<p align='center'><font face='Arial' size='2' color='#000000'>Vlo&#382;te p&#345;íjmení. Bez vlo&#382;eného p&#345;íjmení nelze p&#345;ihlá&#353;ku odeslat.</font></p>");

}

 

if (strlen($surname) == 0 )

{

die("<p align='center'><font face='Arial' size='2' color='#000000'>Vlo&#382;te p&#345;íjmení. Bez vlo&#382;eného p&#345;íjmení nelze p&#345;ihlá&#353;ku odeslat.</font></p>");

}

?>

 

<H2>You write:</H2>

<strong>Name</strong> <?php echo $_POST["name"]; ?><br/>

<strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/>

 

<form action="post2.php" method="post" name="frm">

<input type="hidden" name="name" value="<?php echo $_POST["name"];?>">

<input type="hidden" name="surname" value="<?php echo $_POST["surname"];?>">

<input type="submit" value="submit"/>

</form>

<?php

/*this is my problem */

 

if($susbmit) {

/*connection to DB */

$con = mysql_connect("localhost","root","");

if (!$con)

 {

 die('Could not connect: ' . mysql_error());

 }

 

so I start again and this is my working code:

 

thanks to everyone who help solve this out  ;)

file 1

<html>
<form action="post_2.php" method="post">
<form>
<ul>Name: <input type="text" name="name" /></ul>
<ul>Surname: <input type="text" name="surname" /></ul>
<input type="submit" />
</form>

file 2

<html>
<head>
<style type="text/css" media="screen">@import "css/style2.css";</style>
</head>
<body>
<?php 
/* records check */
if (strlen($name) <2)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name.</font></p>");
}
if (strlen($name) >10)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name</font></p>");
}

if (strlen($name) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name</font></p>");
}
if (strlen($surname) <2)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>");
}
if (strlen($surname) >10)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>");
}

if (strlen($surname) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>");
}
?>
<H2>You wrote:</H2>
<strong>Name</strong> <?php echo $_POST["name"]; ?></br>
<strong>Surname</strong> <?php echo $_POST["surname"]; ?>
<form action="post_jura3.php" method="post" name="frm">
<input type="hidden" name="name" value="<?php echo $_POST["name"];?>">
<input type="hidden" name="surname" value="<?php echo $_POST["surname"];?>">
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>

file 3

<?php
/*connection to DB */
$con = mysql_connect("localhost","root","");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
if ($submit) {
mysql_select_db("formular", $con);
$sql="INSERT INTO formular (name, surname)
VALUES
('$_POST[name]','$_POST[surname]')";
}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<br>Your record was added.";

mysql_close($con) 

?>

</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.