Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call php function from submit button?

Tags:

php

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

like image 338
viki Avatar asked Jul 26 '13 04:07

viki


2 Answers

<?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.

like image 73
Barmar Avatar answered Sep 28 '22 05:09

Barmar


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
}
?>
like image 20
liyakat Avatar answered Sep 28 '22 03:09

liyakat