this is link to code http://jsbin.com/ixupuk/4/edit
when my form is loaded into browser OR refreshed, all fields are auto-submitted to mysql database..
i included at the bottom, an if(isset) statement that i couldnt get to work.
thanx

//submit to database

//  usable constants  
define('DB_NAME', 'yyy_ContactForm');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'zzz');
define('DB_HOST', 'localhost');

$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

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

$db_selected = mysql_select_db(DB_NAME,$link);

if(!$db_selected) {
    die('Can\'t use' . DB_NAME . ':' .mysql_error());
}

//echo 'Connected successfully';

/* $fullname = $_POST['fullname'];
$email = $_POST['email'];
$comment = $_POST['comment']; */

$fullname = mysql_real_escape_string ($_POST['fullname']);
$email = mysql_real_escape_string ($_POST['email']);
$comment = mysql_real_escape_string ($_POST['comment']);

/*~~~~  $error = array(); 
if($_POST[$fullname, $email, $comment] == '') { 
    $error[] = ''; 
}  ~~~~*/

$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', 
'$email', '$comment')";

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

/* this below gave me syntax error
   on the first if() line  

if(isset($_POST)){
  $sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', 
'$email', '$comment')";

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

} 
*/

mysql_close();

originally i thought ppl rather view from well known jsbin, ty

Member Avatar for LastMitch

when my form is loaded into browser OR refreshed, all fields are auto-submitted to mysql database..

I am not sure but I never seen data submited to the database by refreshing the browsers.

Maybe using a cookie? Not sure.

You also need another query to update the data in the mysql database.

I think jQuery might be a better option for your situation.

You can take a look at these code which is in jQuery:

https://bitbucket.org/stanlemon/jquery-autosave/src/d4fa6b11ad79/example.html

and this:

https://github.com/nervetattoo/jquery-autosave

and this:

http://geniuscarrier.com/autosave-a-jquery-plugin/

This code used cookie to save data:

https://github.com/BenGriffiths/jquery-save-as-you-type

hi
google turns up hits for my prob.
seems like they mostly say to add the isset that i tried ,but couldnt get to work.. idk what i did wrong.

Member Avatar for LastMitch

google turns up hits for my prob.seems like they mostly say to add the isset that i tried ,but couldnt get to work.. idk what i did wrong.

Where did you get that info?

Do you understand how a isset() function works?

It means to find or verify rather the variable exist or not.

As you can see you there is more than isset() function to make this code work.

The code is incomplete.

You also need Javascript/jQuery to make this work.

# if(isset($_POST)){
# $sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname',
# '$email', '$comment')";
#
# if (!mysql_query($sql)) {
# die('Error: ' . mysql_error());
# }
#
# } 

ppl reported success by adding the isset where shown. i dont understand why js is needed

Member Avatar for LastMitch

ppl reported success by adding the isset where shown. i dont understand why js is needed

I don't feel like arguing. Base on this code:

if(isset($_POST)){
$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', $email', '$comment')";

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

} 

There's nothing wrong with it. You mention people reporting it works then it works because there's nothing wrong with the code snippet you provided.

Member Avatar for LastMitch

I notice you comment a dozen code lines

Try this code without those commnets then tell me if you got any errors:

define('DB_NAME', 'yyy_ContactForm');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'zzz');
define('DB_HOST', 'localhost');
$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_select_db(DB_NAME,$link);
if(!$db_selected) {
die('Can\'t use' . DB_NAME . ':' .mysql_error());
}
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$comment = $_POST['comment']; 
$fullname = mysql_real_escape_string ($_POST['fullname']);
$email = mysql_real_escape_string ($_POST['email']);
$comment = mysql_real_escape_string ($_POST['comment']);
$error = array();
if($_POST($fullname, $email, $comment) == '') {
$error[] = '';
} 
$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname',
'$email', '$comment')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
if(isset($_POST)){
$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname',
'$email', '$comment')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
}
mysql_close();

sorry. i dont know enough to argue.. i just dont understand things.. i put in your edited code and got this error: :

Function name must be a string in @ this:if($_POST($fullname, $email, $comment) == '') {
  $error[] = '';

i commented out and form loaded..and sent empty fields to database ..
but no errors
tahnx for your response and help

Member Avatar for LastMitch

i put in your edited code and got this error: :

Function name must be a string in @ this:if($_POST($fullname, $email, $comment) == '') {
$error[] = '';

Try this (instead of this ($fullname, $email, $comment) change it to this [$fullname, $email, $comment] ):

if($_POST[$fullname, $email, $comment] == '') {
$error[] = '';

Then run the code again

gives me this:

Parse error: syntax error, unexpected ',', expecting ']' 

which i completely dont understand
tahnx

Member Avatar for LastMitch

Try this:

<?php 
//  usable constants  
define('DB_NAME', 'yyy_ContactForm');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

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

$db_selected = mysql_select_db(DB_NAME,$link);

if(!$db_selected) {
    die('Can\'t use' . DB_NAME . ':' .mysql_error());
}
if ($_POST){
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    $sql = mysql_query("INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', '$email', '$comment')") or die (mysql_error()); 
}
?> 

<form action="yourfile.php" method="post">
Email:<br />
<input type="text" name="email" /><br />
Fullname:<br />
<input type="text" name="fullname" /><br />
comment:<br />
<input type="text" name="comment" /><br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

I modify it a little and when you do submit the form it will insert the data in the database. I tested this code so it works.

thanx very much. i mustve been unclear. my code submits but will do so upon loading the form into the browser .. and so did the code you graciously changed for me.
i also tried to change lil bit to this:

if (isset($_POST)){

it didnt help but neither gave me errors.. thank you

Member Avatar for LastMitch

i mustve been unclear. my code submits but will do so upon loading the form into the browser

What are you doing? You mention this

upon loading , form autosubmits empty fields to database

It means there's no data inserting to the database.

I modify the code because the code you provided doesn't work but you still have errors? What errors? The code I provided works.

This is getting off track. There's nothing much I can do now. The reason is very simple there is nothing wrong with your code to begin with and I end up modifying the code and still you are having issue?

the problem is that name email comments are req'd fields and js and php validate correctly to block submission to my email.

but form will submit to database.. UPON LOADING into browser --whether or not any fields are filled..

if a user partially fills out form , validation will block submission to email and database until all fields are filled.

if all info is supplied then form submits to email + DB, as it should.

Unless user decides (for no good reason) to REFRESH page. then form will send to DB again with whatever boxes are filled or empty.

obviously i was unclear and im sorry for wasting your time..

thanxk very much

Member Avatar for LastMitch

the problem is that name email comments are req'd fields and js and php validate correctly to block submission to my email.

There's must be miscommunication here. Why all of the sudden you mention Javacript?

This is what you said:

ppl reported success by adding the isset where shown. i dont understand why js is needed

I mention to you need javascript which you already have which is this:

window.addEvent('domready', function() {
// Get the form
var form = $('comments_form');
// if the form is found...
if (form) {
// obtain error fields
var name = $('fullname');
var email = $('email');
var comment = $('comment');
// Set the default status
var isValid = true;
// input error function for the error messages
var addError = function (field, msg) {
field.addClass('error'); // Add error class to field
var error = field.getParent().getElement('span') || new Element('span', {'class': 'error'}); // add error message if not already placed
error.set('text', msg); // error text msg
error.inject(field, 'after'); // Insert error message after field
};
// detach error function used to delete any error messages and remove the error class
var removeError = function (field) {
field.removeClass('error'); // Remove error class from form fields
var error = field.getParent().getElement('span'); // find any existing error messages
// destroy if error message
if (error) {
error.destroy();
}
};
// insert submit form event
form.addEvent('submit', function (e) {
// Test name length
if (name.get('value').length === 0) {
isValid = false;
addError(name, nameError);
} else {
isValid = true;
removeError(name);
}
// check email length
if (email.get('value').length === 0) {
isValid = false;
addError(email, emailError);
// check email validity
} else if (!email.get('value').test(/^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/)) {
isValid = false;
addError(email, emailError);
}
//else {
// isValid = true;
// removeError(email);
//}
// check comment length
if (comment.get('value').length === 0) {
isValid = false;
addError(comment, commentError);
}
//else {
// isValid = true;
// removeError(comment);
//}
// If form invalid then stop event happening
if (!isValid) {
e.stop();
}
});
}
});

What are you doing?

obviously i was unclear and im sorry for wasting your time..

Technically not really because everything is in PHP and so far everything is in PHP. If the code was related with Javascript and PHP then things will be different. I feel you haven't been really honest regarding what you are doing. You are mislead me regardin about PHP issue now it is more Javascript issue.

lastmitch. i didnt initiate that my prob was js.. after you said this , "Where did you get that info?

Do you understand how a isset() function works?

It means to find or verify rather the variable exist or not.

As you can see you there is more than isset() function to make this code work.

The code is incomplete.

You also need Javascript/jQuery to make this work."..

i said i dont understand why i would need js for this..i didnt know how else to interpret your statement. i had been trying to make and "if(statement)"work to verify as has been reported by others.. i thought i was doing wrong.

i spose i will take full responsibility for the misunderstandings and if you feel misled, i ll own that as well. i dont know how else to convey my situation so i take the ignorant card too..

i wish i couldda done something better.
hope there are no hard feelings

Such things happens in self submitting page.
I used cookies to solve this kind of problem.
for example when user clicks save before submitting set one cookie say "saveinfo" to 1 , using javascript,

Now along with isset using php $_COOKIE['saveinfo'] ==1,

in the end of page using javascript again to set saveinfo to 0,

Member Avatar for diafol

Never use if(isset($_POST)) as it's always set, even if no form is sent. It will be empty in that case, but still set. You could check to see if a submit button is set or check via !empty.

i will try the !empty .. thanx for looking

I would normally use if(isset($_POST['submit'])){'the rest of your code'} so the form is only submitted if the submit button is clicked, no matter how many times you refresh the page.

@diafol. it seems like the !empty
idea has worked for this: when the form initially, it does not submit to the database. however, filling in one box still lets form submit with empty fields. thank you

@TonyG_cyprus
well , i dont understand how your code line stops empty req'd fields from being submitted (even tho you did not state that it would) but it seems like your simple solution is what worked ..

i think everything is now working as it should, so far as i can tell ..

@urtrivedi cookies would not stop submit form w/empty boxes from being submitted..

i feel like this is solved..

i really appreciate all the attention and responses..

i continue to upgrade to pdo etc...

thanx again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.