Jump to content

Calling a function from form submit


tom11011

Recommended Posts

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!

Link to comment
Share on other sites

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);
}
?>

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.