tom11011 Posted November 21, 2010 Share Posted November 21, 2010 Hello, I am a newbie to php. I am trying to figure out how to call a function when a user clicks submit on a form. I have a sample script that I am writing where the user inputs there name, address, etc.. and then clicks submit and it inserts into the mysql database. I have successfully done this with form action to my adduser.php file, but I would rather have a single function file with adduser, delete a user, edit a user, etc.. here is my form <form action="includes/customersfunctions.php?add" method="post"> name: <input type="text" name="name" /><br /> address: <input type="text" name="address" /><br /> city: <input type="text" name="city" /><br /> state: <input type="text" name="state" /><br /> zip: <input type="text" name="zip" /><br /> <input type="submit" /> </form> Here is my function <?php function add() { include("config.php"); mysql_query("INSERT INTO customers (name,address,city,state,zip) VALUES ('name','address','city','state','zip') ") or die(mysql_error()); echo "added"; mysql_close($con); } ?> The function will have delete, update, etc.. later Thanks for your help in advance! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 21, 2010 Share Posted November 21, 2010 a rather long-winded example: form: <form action="includes/customersfunctions.php" method="post"> name: <input type="text" name="name" /><br /> address: <input type="text" name="address" /><br /> city: <input type="text" name="city" /><br /> state: <input type="text" name="state" /><br /> zip: <input type="text" name="zip" /><br /> <input type="submit" /><input type='hidden' name='add' value='1'> </form> customersfunctions.php: <?php if (isset($_POST['add']) && intval($_POST['add']) == 1) { include("config.php"); $name = (isset($_POST['name']))?$_POST['name']:""; $address = (isset($_POST['address']))?$_POST['address']:""; $city = (isset($_POST['city']))?$_POST['city']:""; $state = (isset($_POST['state']))?$_POST['state']:""; $zip = (isset($_POST['zip']))?$_POST['zip']:""; if (get_magic_quotes_gpc()) { $name = stripslashes($name); $address = stripslashes($address); $city = stripslashes($city); $state = stripslashes($state); $zip = stripslashes($zip); } $name = mysql_real_escape_string($name); $address = mysql_real_escape_string($address); $city = mysql_real_escape_string($city); $state = mysql_real_escape_string($state); $zip = mysql_real_escape_string($zip); $sql = "INSERT INTO customers (name,address,city,state,zip) VALUES ('$name','$address','$city','$state','$zip') "; mysql_query($sql) or die(mysql_error()); echo "added"; mysql_close($con); } ?> Quote Link to comment Share on other sites More sharing options...
tom11011 Posted November 21, 2010 Author Share Posted November 21, 2010 I got a 500 error and the data did not insert. The page redirected to /includes/customersfunctions.php which I did not expect. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 22, 2010 Share Posted November 22, 2010 the form action is action="includes/customersfunctions.php" that is where the form data is submitted. if you would like it submitted to a different file, you'll need to update the form action. Quote Link to comment 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.