melbahtoast Posted January 14, 2013 Share Posted January 14, 2013 Hi all! I'm fairly new to php, don't really know the proper ins and outs but I'm learning. I'm stuck on two things. I have a simple form that submits an entered e-mail address to my email and also kicks back to a database. That part works, however, when ever I go to the page or hit the "submit" button it generates a blank row in the database. Also (problemo #2), the submit button should echo "Please enter your email" when the submit button is hit with nothing in the field, but it doesn't. I've pulled the code and put it on a new file and it works, maybe it's just something else I have on the page that's interfering? The form is same page, it echos below the e-mail input success or invalid e-mail. Using bootstrap if that helps at all. Here's the code: <form name="emailcapture" method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <input name="email" type="text" value="<?php if(isset($_POST['submit'])){echo $email;} ?>" class="emailform"/> <?php if(isset($_POST['submit'])){echo $erre;} ?> <input type="submit" name="submit" value="Submit" class="btn"/><br><span class="formvalidate"><?php$link = mysql_connect('localhost', 'username', 'password');if (!$link) { die('Could not connect: ' . mysql_error());} if(isset($_POST['submit'])){ $email=$_POST['email']; $to="[email protected]"; $re="Sign Up"; $msg = "$email"; $headers = "From: $email\r\n"; if(!$email){ $erre="Please enter your email."; }else{ $email = trim($email); $_ename = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}~]+"; $_host = "([0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if ( !preg_match($_ename."@".$_host.$_tlds,$email)){ echo "Sorry, it appears the e-mail address you have entered is not valid, try again."; }else{ mail($to, $re, $msg, $headers); echo "Thank you. You have been added to our list and will be notified when we launch."; } } }mysql_select_db('database');mysql_query("INSERT INTO table (emails, date) VALUES ('$_POST[email]', now())"); mysql_close($link) ?></span> </form> Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/ Share on other sites More sharing options...
melbahtoast Posted January 14, 2013 Author Share Posted January 14, 2013 (edited) - 5.0.96 - $link = mysql_connect('localhost', 'username', 'password');if (!$link) { die('Could not connect: ' . mysql_error());} mysql_select_db('database');mysql_query("INSERT INTO table (emails, date) VALUES ('$_POST[email]', now())"); mysql_close($link) - no errors outputted - tried moving the code around some but nothing worked. I think it would work if I just threw it on a separate php page an changed the action on the form, but ideally I would like this all to work on one page, I don't want the page to refresh at all. Also, replaced the code with "username", "password" etc. Sorry for the lack of info, like I said I'm kind of new to this... Edited January 14, 2013 by melbahtoast Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/#findComment-1405717 Share on other sites More sharing options...
TRI0N Posted January 15, 2013 Share Posted January 15, 2013 For starters I see this '$_POST' which should be $_POST['email'] also email tends to be a script call and usually never works on a lot of out of box setups. So change that to $_POST['e_mail'] and all references of email to e_mail. Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/#findComment-1405952 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 (edited) There is nothing wrong with using "email" as the index of the POST array, nor as the name of input fields. There are absolutely 0 side effects of doing so, with any input field[1]. It's great that you want to help people, but please ensure that what you're posting is accurate and actual facts. Otherwise you're not helping, but only making things worse. So, please, take the time to verify that what you think is the case is actually correct, before posting. melbahtoast: Add some proper indentation and newlines to your script, so it's possible to read it. Remember, the easier you make it for us to help you, the higher the chance of you getting (quality) help. Also, since you're using PHP_SELF, you should check out this snippet. [1]With the possible exception of the naming the submit type "submit", but only for a certain old, and long outdated, browser. And then only in very specific uses of Javascript. Edited January 16, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/#findComment-1406045 Share on other sites More sharing options...
melbahtoast Posted January 20, 2013 Author Share Posted January 20, 2013 Hi everyone - decided to go a different route but thanks for the feed back. Not sure whether to mark this solved or not... Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/#findComment-1407009 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2013 Share Posted January 20, 2013 You would mark it solved, so that someone won't take the time to reply in it. Also, I notice that this is in the mysql forum section, but it's not specifically a mysql query problem, so moving it to the php help forum section... Quote Link to comment https://forums.phpfreaks.com/topic/273115-new-row-in-sql-when-page-loads/#findComment-1407012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.