5

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

0

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

'post' is no more secure than 'get'
And even if it is, it shouldn't affect whether the script runs correctly.
@Dagon , why it would be?
It may be a reasononable suggestion, but it won't fix the problem. It should be a comment, not an answer.
he can solve this problem with use of my answer, that is nothing wrong
1

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

3 Comments

That's a pretty significant if.
@Barmar You forgot ) in first if and () in elseif. :)
@Barmar See another same question from same user stackoverflow.com/questions/17873067/…
1

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

1 Comment

He's using GET, since that's the default. And $_REQUEST combines the contents of $_GET and $_POST, so it doesn't matter which method he uses. How is this an "answer"?
0
<?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;
}
?>

1 Comment

Quite rightly it has already been pointed out, that you are using reserved words as a function name. Simply rename your functions to something like button_insert(), button_select() or whatever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.