简体   繁体   中英

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM