It is currently Thu Aug 18, 2022 7:38 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [PHP] $PHP_SELF Form to add record to MYSQL
PostPosted: Mon Aug 28, 2006 4:59 am 
Noobie
Noobie

Joined: Wed Aug 23, 2006 6:17 am
Posts: 20
I have two simple programs which work fine. One is addcontact.html, which is a form inputs the fields, and then submits to addcontact.php, which adds the fields to a mysql table.

Quote:

code for addcontact.php

<form action="addcontact.PHP" method="post">
company=== : <input type="text" name="company"><br>
prefix==== : <input type="text" name="prefix"><br>
firstname= : <input type="text" name="firstname"><br>
lastname== : <input type="text" name="lastname"><br>
street==== : <input type="text" name="street"><br>
city====== : <input type="text" name="city"><br>
zip======= : <input type="text" name="zip"><br>


phone===== : <input type="text" name="phone"><br>
phoneext== : <input type="text" name="phoneext"><br>

fax======= : <input type="text" name="fax"><br>
faxext==== : <input type="text" name="faxext"><br>
salutation : <input type="text" name="salutation"><br>
altphone== : <input type="text" name="altphone"><br>
website=== : <input type="text" name="website"><br>
email===== : <input type="text" name="email"><br>
type====== : <input type="text" name="type"><br>

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



Quote:
code for addcontact.php

<?php

include("/home/data.php");

$conn = mysql_connect("$location","$username","$password");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db($database,$conn) or die ("Could not open database");



$fullname = $firstname . " " . $lastname ;


$insert = "INSERT INTO contact (company, fullname, street, city, state, zip, phone, fax,
salutation, altphone, firstname, lastname, phoneext,faxext, website, email, type, prefix
)
VALUES ('$company', '$fullname', '$street', '$city', '$state',
'$zip', '$phone', '$fax', '$salutation', '$altphone', '$firstname', '$lastname', '$phoneext', '$faxext',
'$website', '$email', '$type', '$prefix')";

mysql_query($insert) or die ("Could not add data to the table");



echo "<p>";

echo "================================== Contact Added";

echo "<p>";
echo ("$company<BR>");
echo ("$fullname");


?>





What I would like to do is combine these two programs into ONE php program which calls itself repeatedly, via $PHP_SELF, and can sense when one or more input fields are empty, and then exit to the calling menu.

I am having a very difficult time. I am searching up and down google for examples.

What I really need is ONE SIMPLE COMPLETE WORKING PROGRAM as an example which I can use as a model.

If you give me anything else other than one complete example program, then it will not help me one bit. I am not that bright, so it will not help me to give me hints, or ask me why I really want to do this, or give me an introductory lecture to software design.

I have several books, and they both give the simple example of that number guessing game, which keeps calling itself until you guess the correct number, using $PHP_SELF.

Part of my reason for wanting to combine these two programs into one is that I want to use cookieless sessions, which I have got working, if I call a php script. But if I call an html script, I am not certain if I can pass the sessionid to html. But even if you have some way for me to pass the session id to html, I still would like a working example of a php script that calls itself repeatedly and adds records to a mysql table, until the user indicates that he is finished, or until certain fields are blank.

I was puzzled how to clear out the fields in the form each time, until I discovered the reset button

Quote:
<INPUT TYPE="reset">


I feel that if only a simple person like myself could find a handful of examples to accomplish certain tasks in php/mysql, then I could make progress in writing my own simple but useful system for contact management (similar to ACT! Contact Management, but much simpler).

I suppose it would be nice if I could find a ready made shareware system, that I could install and study. I would definitely try such a download out. But still, I feel that if I can struggle through the exercise of mastering some of these techniques, then I will have much better control and understanding over the simple system which I do write.

Thanks in advance, if you can make it possible for me to succeed in my project of teaching myself basic php/mysql.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:13 am 
Noobie
Noobie

Joined: Wed Aug 23, 2006 6:17 am
Posts: 20
Here is a tutorial which ALMOST shows me exactly the example I need, but it does not bother to show the ENTIRE CODE example, and I cannot figure out how to follow their example to produce the actual code:

http://www.tizag.com/phpT/examples/formex.php

they just sort of SUGGEST how one might complete the code, and I am lost.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:19 am 
Noobie
Noobie

Joined: Wed Aug 23, 2006 6:17 am
Posts: 20
http://www.linuxguruz.com/z.php?id=33

I might be able to get something working from THIS example from the above link:

Quote:

<form action="<?= $PHP_SELF ?>">
<?
if (!isset($subject)) {
echo "Fill out and submit the form below.<P>"; }
if (!isset($subject[0]) || $subject[0] == ""
|| $subject[0] == "Enter your Full Name") {
$subject[0] = "Enter your Full Name"; echo ?>
<input type="text" NAME="subject[0]"
value="<?= $subject[0] ?>" SIZE="50" /><br />
<? } else { echo ?>
<input type="hidden" NAME="subject[0]" value="<?= $subject[0] ?>" />
<? } if (!isset($subject[1]) || $subject[1] == ""
|| $subject[1] == "Enter your Street Address") {
$subject[1] = "Enter your Street Address"; echo ?>
<input type="text" NAME="subject[1]"
value="<?= $subject[1] ?>" SIZE="50" /><br />
<? } else { echo ?>
<input type="hidden" NAME="subject[1]" value="<?= $subject[1] ?>" />
<? } if (!isset($subject[2]) || $subject[2] == ""
|| $subject[2] == "Enter your E-Mail Address") {
$subject[2] = "Enter your E-Mail Address"; echo ?>
$subject[2] = "Enter your E-Mail Address"; echo ?>
<input type="text" NAME="subject[2]"
value="<?= $subject[2] ?>" SIZE="50" /><p />
<? } else { echo ?>
<input type="hidden" NAME="subject[2]" value="<?= $subject[2] ?>" />
<? }
if ($subject[0] == "Enter your Full Name" ||
$subject[1] == "Enter your Street Address" ||
$subject[2] == "Enter your E-Mail Address") { echo ?><P>
<input type="submit" value="Submit!" /><input type="reset" value="Reset" />
<? } else { echo "Thank You $subject[0]!"; } ?>
</form>


I think I kind of understand what is going on here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:37 am 
Noobie
Noobie

Joined: Wed Aug 23, 2006 6:17 am
Posts: 20
The examples at this link seem to be working. It is quite late now. I must study them in the morning.

http://www.phpfreaks.com/phpref/34.php

Quote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>All-In-One Feedback Form</title>
</head>
<body>
<? //Create form
$form_block = "<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your Name:</strong><br>
<input type=\"text\" name=\"sender_name\" value=\"$_POST[sender_name]\" size=30></P>
<P><strong>Your E-Mail Address:</strong><br>
<input type=\"text\" name=\"sender_email\" value=\"$_POST[sender_email]\" size=30></P>
<P><strong>Message:</strong></br>
<textarea name=\"message\" cols=30 rows=5 wrap=virtual>$_POST[message]</textarea></p>
<input type=\"hidden\" name=\"op\" value=\"ds\">
<P><input type=\"submit\" name=\"submit\" value=\"Send This Form\">
<input type=\"reset\" name=\"reset\" value=\"Reset This Form\"></p>
</form>";
if ($_POST[op] != "ds")
{
// they need to see the form
echo "$form_block";
}
else if ($_POST[op] == "ds")
{
// check value of $_POST[sender_name]
if ($_POST[sender_name] == "")
{
$name_err = "<font color=red>Please enter your name!</font><br>";
$send = "no";
}
// check value of $_POST[sender_email]
if ($_POST[sender_email] == "")
{
$email_err = "<font color=red>Please enter your e-mail address!</font><br>";
$send = "no";
}
//check value of $_POST[message]
if ($_POST[message] == "")
{
$message_err = "<font color=red>Please enter a message!</font><br>";
$send = "no";
}
if ($send != "no")
{
// it's ok to send, build mail

$to = "bertis (at) fidmail.com";
$subject = "All-in-One Web Site Feedback\r\n";
$mailheaders = "From: My Web Site <> \r\n";
$mailheaders .= "Reply-To: $_POST[sender_email]\r\n\r\n";

$msg = "E-MAIL SENT FROM WWW SITE\r\n";
$msg .= "Sender's Name: $_POST[sender_name]\r\n";
$msg .= "Sender's E-Mail: $_POST[sender_email]\r\n";
$msg .= "Message: $_POST[message]\r\n\r\n";
// send the mail
mail($to, $subject, $msg, $mailheaders);
//display confirmation to user
echo "<P>Mail has been sent!</p>";
}
else if ($send == "no")
{
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
?>

</body>
</html>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 4:37 am 
Support Team Member

Joined: Sun Jun 11, 2006 5:16 pm
Posts: 195
:wink:

_________________
Bob
support@100webspace.com
http://100webspace.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ]  Moderators: KJ, fhmagic, Support Team, Moderators

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
100WebSpace © 2011