Jump to content

I am trying to create a simple php project. (need help)


josuenerd

Recommended Posts

Yea, just take your PHP and paste it to the top of the submit.php page.

 

Then, on your form, change your action.  action=""

 

Before your PHP add this

 

if(isset($_POST['submit_btn']))

{

PHP GOES HERE

}

 

change submit_btn to whatever name your button is.  This check to see if the button is pressed and if so, execute the code.

Link to comment
Share on other sites

I think I did something wrong.  I tried to follow instructions but now it just shows this submit form and redirects back to the submit form.

 

submit.php

 

<?php

if(isset($_POST['submit_button']))
{
    
    
$con = mysql_connect("localhost","trend_learnu","asdfasdf");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("trend_learningdb", $con);

$sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date)
VALUES
('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



$db = mysql_connect("localhost","trend_learnu","asdfasdf");
mysql_select_db("trend_learningdb",$db);

$result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC");

$table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="207" bgcolor="#CCCCCC">Firstname</td>
    <td width="204" bgcolor="#CCCCCC">Lastname</td>
    <td width="197" bgcolor="#CCCCCC">Weight</td>
    <td width="197" bgcolor="#CCCCCC">Date</td>
  </tr>';

while($row=mysql_fetch_array($result))
{
  $firstname = $row['Firstname'];
  $lastname = $row['Lastname'];
  $weight = $row['Weight'];
  $date = $row['Date'];

   $table.= '<tr>';
   $table.= '<td>'.$firstname.'</td>';
   $table.= '<td>'.$lastname.'</td>';
   $table.= '<td>'.$weight.'</td>';
   $table.= '<td>'.$date.'</td>';
   $table.= '</tr>';
}
$table .= '</table>';

echo $table;



mysql_close($con)



?> 


<html>
<body>
<?php
$today = date("F j, Y");

}

?> 


<form action="" method="post">
<input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" />
First Name: <input type="text" name="Firstname" /><br><br>
Last Name: <input type="text" name="Lastname" /><br><br>
Weight: <input type="text" name="Weight" /><br><br>

<input name="button" type="submit" value="Submit Profile" />
</form>



</body>
</html> 

Link to comment
Share on other sites

Try something like that.  Remember, I can't test this.

 

 

<?php
$con = mysql_connect("localhost","trend_learnu","asdfasdf");

if (!$con)    {die('Could not connect: ' . mysql_error());}

mysql_select_db("trend_learningdb", $con);

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

$db = mysql_connect("localhost","trend_learnu","asdfasdf");
mysql_select_db("trend_learningdb",$db);


$result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC");

$table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="207" bgcolor="#CCCCCC">Firstname</td>
    <td width="204" bgcolor="#CCCCCC">Lastname</td>
    <td width="197" bgcolor="#CCCCCC">Weight</td>
    <td width="197" bgcolor="#CCCCCC">Date</td>
  </tr>';

while($row=mysql_fetch_array($result))
{
  $firstname = $row['Firstname'];
  $lastname = $row['Lastname'];
  $weight = $row['Weight'];
  $date = $row['Date'];

   $table.= '<tr>';
   $table.= '<td>'.$firstname.'</td>';
   $table.= '<td>'.$lastname.'</td>';
   $table.= '<td>'.$weight.'</td>';
   $table.= '<td>'.$date.'</td>';
   $table.= '</tr>';
}
$table .= '</table>';


if(isset($_POST['submit_btn']))
{
    $firstname = $_POST['Firstname'];
    $lastname = $_POST['Lastname'];
    $weight = $_POST['Weigh'];
    
    mysql_query("INSERT INTO Peoples VALUES('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')");
    
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="" method="post">
<input name="Date" type="hidden" value="<?php $today; ?>" />
First Name: <input type="text" name="Firstname" /><br><br>
Last Name: <input type="text" name="Lastname" /><br><br>
Weight: <input type="text" name="Weight" /><br><br>

<input name="submit_btn" type="submit" value="Submit Profile" id="submit_btn" />
</form>
<p> </p>
<?php if(isset($table)) {echo $table;} ?>
</body>
</html>

Link to comment
Share on other sites

Below is the code I am using. The forum is under the tables how i wanted. 

 

All I want to do now is make it so that the form does not submit if (Weight) is left blank. Maybe showing a error message. This should stop some spam.

 

insert.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Weight Log</title>
</head>

<body>


<?php
$con = mysql_connect("localhost","learnu","asdfasdf");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("learning", $con);

$sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date)
VALUES
('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



$db = mysql_connect("localhost","learnu","asdfasdf");
mysql_select_db("learning",$db);

$result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC");

$table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="207" bgcolor="#CCCCCC">Firstname</td>
    <td width="204" bgcolor="#CCCCCC">Lastname</td>
    <td width="197" bgcolor="#CCCCCC">Weight</td>
    <td width="197" bgcolor="#CCCCCC">Date</td>
  </tr>';

while($row=mysql_fetch_array($result))
{
  $firstname = $row['Firstname'];
  $lastname = $row['Lastname'];
  $weight = $row['Weight'];
  $date = $row['Date'];

   $table.= '<tr>';
   $table.= '<td>'.$firstname.'</td>';
   $table.= '<td>'.$lastname.'</td>';
   $table.= '<td>'.$weight.'</td>';
   $table.= '<td>'.$date.'</td>';
   $table.= '</tr>';
}
$table .= '</table>';

echo $table;



mysql_close($con)


?> 







<?php
$today = date("F j, Y");
?> 



<table width="500" border="0" align="center" cellpadding="10" cellspacing="0">
  <tr>
    <td>
    <form action="insert.php" method="post">
<input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" />
First Name: <input type="text" name="Firstname" /><br><br>
Last Name: <input type="text" name="Lastname" /><br><br>
Weight: <input type="text" name="Weight" /><br><br>

<input type="submit" value="Submit Profile" />
</form>
</td>
  </tr>
</table>



</body>
</html>

Link to comment
Share on other sites

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.