Jump to content

php forms


Ninjakreborn

Recommended Posts

I have created a php form, I am playing around trying to create a mailing list, the question I have is, when I create a form, with 2 submit buttons
subscribe and unsubscribe
there are three field intries
emailaddress
firstname
lastname
as well as 3 column's in 1 table, out of my database
emailaddress
firstname
lastname
I want to have if they fill in the form(it's already validate with javascript and php)
How do I get it to where, the subscribe button adds there email first and last name to the database
but if they unsubscribe it removes there emailaddress, last name and first name from the database, if it's present.
I also have to figure out how to get a mysql page added into the php script, for instance I was told to create a php page with all my mysql stuff, connecting to the database, adding, taking out the data, ex cetera, and to have it included in another page that does the validation. So i have my general.js file validate it, then the mailinglist.php validate it server side,
with an if else statement
[code]
<?php
if ($_POST['emailaddress'] == "") {
print('I am sorry please go back and fill in the email address field properly');
}elseif ($_POST['firstname'] == "") {
print('I am sorry please go back and fill in the first name correctly');
}elseif ($_POST['lastname'] == "") {
print('I am sorry please go back and fill in the last name correctly');
}else {
return true;
include(/home/freelan4/public_html/cgi-bin/mailinglist/dbconnector.php);
}
?>
[/code]
does that work or is there another way, I have create nothing in the dbconnector.php file except for connecting to my server and putting in my username and password, with a die or something this is what I have in my other page now, I haven't started getting around to making it add and take out the information yet
[code]
<?php
$dbh=mysql_connect ("localhost", "ihavemyusernamehere", "<ihavemypasswordhere>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("freelan4_mailinglist");
?>
[/code]
I just want some general advice, I created a test database at
www.freelancebusinessman.com
you can see the newsletter sign up on the homepage, I will probably be removing the whole thing, but I wanted to create it completely, and create another file for me to log into, and send the emails, so I can test it, once I build a full one, then I will understand all the basics of working with databases and how mailing lists work, that is the reason I am playing, any guidance or help with any of this would be greatly appreciated, I put it in the php section because it was mostly about php.
Link to comment
Share on other sites

When I validate forms with php I tend to do this:

[code]

if(!empty($_POST['emailaddress'])) {
   // set email address varible here
} else {
  // Email field was left blank
}
[/code]

Then when you want to insert I would do:

[code]
//Do all validation above this

$strQry = "INSERT INTO table (id, emailaddress, firstname, lastname) VALUES (NULL, '{$emailaddress}', '{$firstname}', '{$lastname'])";

$query = mysql_query($strQry) or die("MySQL Error: {$strQry} <br /> ". mysql_error());
// Continue on....
[/code]
Link to comment
Share on other sites

ok but I don't get it, what's the difference in those 2 validation methods, I was using this one, so I can chain them together in a huge if, ifelse, else construct, that way it runs down the line.
I am still confused about the mysql intry.
I just do all the validation then below that on the same page do all of my mysql work.
IS there a way to store the mysql information in another page, and if I do that on the same page, will it validate it, and then if it passes validation do the database work, or will it do the validation and that at the same time, meaning will it validate and come back that it wasn't filled in correctly and still pop the information in the database.
Link to comment
Share on other sites

[!--quoteo(post=377414:date=May 26 2006, 02:35 PM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ May 26 2006, 02:35 PM) [snapback]377414[/snapback][/div][div class=\'quotemain\'][!--quotec--]
ok but I don't get it, what's the difference in those 2 validation methods, I was using this one, so I can chain them together in a huge if, ifelse, else construct, that way it runs down the line.
I am still confused about the mysql intry.
I just do all the validation then below that on the same page do all of my mysql work.
IS there a way to store the mysql information in another page, and if I do that on the same page, will it validate it, and then if it passes validation do the database work, or will it do the validation and that at the same time, meaning will it validate and come back that it wasn't filled in correctly and still pop the information in the database.
[/quote]

The difference between the two methods is not too much. The one I posted checks to see if the $_POST is empty, like yours which is $_POST == "".

I do it with all seperate if/else statements because it makes it neater for me to see where my $_POST variables are and what they are doing. Sometimes my validation can get big because to do parsing of the data if its valid in the same part.

I usually do my validation and MySQL stuff on the same page. LIke:
[code]

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

   // User submitting things get the variables and validate them
  
   // Create MySQL Query and execute it.
} else {
   // USer has not submitted the form

   // Display the form to them so they can fill it out.
}
[/code]

You can make a seperate page handle the MySQL stuff. You can either require() it or just change the action of your HTML form to point to the new page.

As for the MySQL query.
[code]
$strQry = "INSERT INTO table (id, emailaddress, firstname, lastname) VALUES(null, '{$emailaddress}', '{$firstname}', '{$lastname}')";
[/code]

$strQry is a string variable to use later when executing the query

INSERT INTO table <-- Table is the name of the table you want to put the information into

id, emailaddress, firstname, lastname <-- Are the fields within the table

null, '{$emailaddress}', '{$firstname}', '{$lastname}' <-- values to put into the fields respectively.

null is a blank instance so that the MySQL Table fills it in automatically (I use ID as a unique auto-increment field) for my primary key. Helps search a bit faster

$emailaddress is the variable you have gotten from the HTML form ie $_POST['emailaddress']

I always put the variables betewwn curley braces in my MySQL Queries. The single quotes around those variables are to distinguish between fields in the query.

Whew I hope this makes somewhat sense....

[code]
$query = mysql_query($strQry) or die("MySQL Error: <br /> {$strQry} <br />". mysql_error();
[/code]

mysql_query() <--- function to execute the actual MySQL Query. You feed it the $strQry string.

or die() <-- If there is something amiss with the query, it will kill (stop) the script.

mysql_error() <-- if there is an error it will tell you what the error is

"MySQL Error: <br /> {$strQry} <br /> ". mysql_error(); <-- Something I do when something goes wrong. It will print that line. Show you the string(query) you wanted to execute and then tell you what is wrong with it.

Makes debugging queries a bit easier.

*wipes forehead*

Does that help? I can try and explain it a bit more if not :)
Link to comment
Share on other sites

Whoah actually that was a major help, I won't be able to put all of this into practice until monday, I will be starting on this monday, and trying to see if I can get it to work, I commented out my form for now, I will work on it starting monday, I am deleting it afterwards but I wanted to learn the basics of Mysql-PHP, so I can start doing databasing better, after that I am going to rebuild it 4-5 times until I master the basic aspects. I will start working with all of this on monday, if I have any more questions about this specific stuff, I will ask on monday after I test this out, I will use this same post, I really appreciate the help so far, thanks alot.

I do have one more question, that I was wondering about, I understand this, I won't get a grasp on it until I play with it monday but I understand it, what I don't understand is what happens if I have a form for a mailing list, they have 2 submit buttons subscribe, and unsubscribe, I still want them both to validate, but I want subscribe specifically to add the emailaddress, firstname, lastname, and I want unsubscribe to remove the emailaddress, firstname, lastname from the database IF it is present, but if it's not present say that they were not subscribed. What I don't understand is how to get 2 actions onto 1 form, because I know I can send the information to a page, and ahve it validate and process, or database or whatever the information, but I don't understand how to get the unsubscribe to still validate and if unsubscribe was pressed instead of intering the information it removes the information from the database, supposing that the information was present.
Link to comment
Share on other sites

Well with your two submit buttons they are named different correct?
[code]
<input type="submit" value="Subscribe" name="subscribe" />
<input type="submit" value="Unsubscribe" name="unsubscribe" />
[/code]

Then you just check to see which one was pressed:
[code]
if(isset($_POST['subscribe'])) {
  // User wants to subscribe so lets create a INSERT INTO query
  $strQry = "INSERT INTO tables(blah, blah2, blah3) VALUES(null, '{$blah}', '{$blah2}', '{$blah3}')";
  $query = mysql_query($strQry) or die(mysql_error());
  // Succsss if not killed..
} elseif (isset($_POST['unsubscribe'])) {
    // User wants to unsubscribe, so lets Delete them!
   $strQry = "DELETE FROM table WHERE emailaddress = '{$emailaddress'] LIMIT 1";
   $strQry = mysql_query($strQry) or die(mysql_error());
   // Success if not killed
} else {
   // Display form to user to fill out
}
[/code]

In the DELETE FROM table statement, I use the LIMIT 1; Which will only delete 1 instance of what it finds (in case there are multiple email address that are the same). If there is more than one of the same address without LIMIT 1 it will delete them all ;)

Link to comment
Share on other sites

I can't understand this now, but thanks for the help on monday I will go back over all of this, pick up the pieces and start putting it all together, thanks to these examples I think next week I can gain a deep working knowledge of databasing, thanks for the help again, I have created database before, but for fun, never using them through php, I will work with this, thanks again for the help, if I run into any more questions about this specifically I will ask them in the same post, thanks again for the help.
Link to comment
Share on other sites

I am trying to set all my form field values as an array, and test them all for blank spaces at once, but I am unsure how to put them all into an array.
I tried
[code]
<?php
$formfields = array("$_POST['firstname']"=>1, "$_POST['lastname']=>2, "$_POST['emailaddress']=>3, "$_POST['verifyemail']"=>4);
foreach ($formfields == ''){
print('One of the fields have been left blank, Please go back and correct this');
}
?>
[/code]
but I got this
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/freelan4/public_html/programmingplayground/php/mailinglist/dbmanagerprocessor.php on line 2

[/quote]
and line 2, was during the middle of me setting my array.

I caught one error, missed some quotations marks but it still gives the same error.
Link to comment
Share on other sites

[!--quoteo(post=378091:date=May 29 2006, 08:39 AM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ May 29 2006, 08:39 AM) [snapback]378091[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I am trying to set all my form field values as an array, and test them all for blank spaces at once, but I am unsure how to put them all into an array.
I tried
[code]
<?php
$formfields = array("$_POST['firstname']"=>1, "$_POST['lastname']=>2, "$_POST['emailaddress']=>3, "$_POST['verifyemail']"=>4);
foreach ($formfields == ''){
print('One of the fields have been left blank, Please go back and correct this');
}
?>
[/code]
but I got this

and line 2, was during the middle of me setting my array.

I caught one error, missed some quotations marks but it still gives the same error.
[/quote]

Try:
[code]
// Setting the first entry to 1 will set the rest 1..2..3.. etc :)

$formfields = array(1=> $_POST['firstname'],  $_POST['lastname'], $_POST['emailaddress'], $_POST['verifyemail']);

// Go through each value in the array
foreach($formfields as $value) {
  if($value == "") {
     // Value is blank
      echo "{$value} is blank<br />";
  } else {
     // Value is not blank
  }
}
[/code]
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.