viki
viki

Reputation: 203

how to call php function from submit button?

my filename is contacts.php that have two submit buttons;i want that if insert button is pressed insert function is called and if select is pressed select is called.i have written following code:

//contacts.php
<?php
 if(isset($_REQUEST['select']))
{
    select();
}
else
{
    insert();
}
?>

<html>
<body>
<form action="contacts.php">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>

<?php
function select()
{
   //do something
}
function insert()
{
   //do something
}
?>

but it is not working .please help

Upvotes: 5

Views: 72604

Answers (5)

Matthew Herbst
Matthew Herbst

Reputation: 32053

As has been described by several people (summarizing the previous comments), you have two options.

The first is to send the data via POST or GET to the server directly and reserve (refresh) the page based on whatever you do inside select() and insert().

While this is not the right place for a POST v GET discussion, convention is to use POST when sending data to the server. POST is slightly more secure because the information is not stored in the browser. Read more about the two here: http://www.w3schools.com/tags/ref_httpmethods.asp

The second option is to use AJAX to accomplish your task without refreshing the web page. In short, AJAX uses Javascript methods that you place on your page to communicate with your server, thus avoiding the need for the PHP on the server to actually change anything on the page (which would require a refresh). A code example of AJAX can be found here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

Upvotes: 1

user1263254
user1263254

Reputation:

<?php
$insert = $_POST['insert'];
$select = $_POST['select'];

if ($insert) {
insert();
}

if ($select) {
select();
}

else {
echo 'press any button...';
}
?>

<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>

<?php
function select() {
echo 'you pressed the [select] button';
exit;
}

function insert() {
echo 'you pressed the [insert] button';
exit;
}
?>

Upvotes: 0

Barmar
Barmar

Reputation: 782785

<?php
if (isset($_REQUEST['insert'])) {
    insert();
} elseif (isset($_REQUEST['select'])) {
    select();
}

Your code is calling insert() even if no button is clicked, which will happen when the page is first displayed.

Upvotes: 12

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

If you are using return inside function to return the result , you have to use echo to print the result while calling function.

if(isset($_REQUEST['select']))
{
    echo select();
}
elseif(isset($_REQUEST['insert']))
{
    echo insert();
}

Upvotes: 1

liyakat
liyakat

Reputation: 11853

use post method because it is secure

//contacts.php
<?php
 if(isset($_POST['select']))
{
    select();
}
else
{
    insert();
}
?>

<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>

<?php
function select()
{
   //do something
}
function insert()
{
   //do something
}
?>

Upvotes: 1

Related Questions