1

I want to call php function in form action and i want to pass id as a argument. What I am doing is, in html form database column values will be displayed in text boxes, If I edit those values and click 'update' button values in database should be updated and 'Record updated successfully'message should be displayed in same page. I tried below code but not working. Let me know the solution. Thanks in advance.

    <html>

    <head>

        <link rel="stylesheet" type="text/css" href="cms_style.css">        


    </head>

    <?php
        $ResumeID = $_GET['id']; 
        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("engg",$con);
        $sql="SELECT * from data WHERE ResumeID=$ResumeID";
        $result = mysql_query($sql);
        $Row=mysql_fetch_row($result); 

        function updateRecord()
        {

            //If(!isset($_GET['id']))       
            //{
              $NameoftheCandidate=$_POST[NameoftheCandidate];
              $TelephoneNo=$_POST[TelephoneNo];
              $Email=$_POST[Email];

      $sql="UPDATE data SET NameoftheCandidate='$_POST[NameoftheCandidate]',         TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]' WHERE ResumeID=$ResumeID ";

            if(mysql_query($sql))
                echo "<p>Record updated Successfully</p>";
            else
                echo "<p>Record update failed</p>";



            while ($Row=mysql_fetch_array($result))     {
                echo ("<td>$Row[ResumeID]</td>");

                echo ("<td>$Row[NameoftheCandidate]</td>");
                echo ("<td>$Row[TelephoneNo]</td>");
                echo ("<td>$Row[Email]</td>");
            } // end of while

        } // end of update function


    ?>

    <body>
        <h2 align="center">Update the Record</h2>

        <form align="center" action="updateRecord()" method="post">
            <table align="center">
                <input type="hidden" name="resumeid" value="<? echo    "$Row[1]"?>">
            <? echo "<tr> <td> Resume ID </td>   <td>$Row[1]</td> </tr>" ?>                      

        <div align="center">
        <tr>       
        <td> Name of the Candidate</td>
    <td><input type="text" name="NameoftheCandidate" 
size="25" value="<? echo "$Row[0]"?     >"></td>
        </tr>

        <tr>
     <td>TelephoneNo</td>
    <td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[1]"?>"></td>
     </tr>
     <tr>       
      <td>Email</td>
        <td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
        </td>
    </tr>
     <tr>
    <td></td>
    <td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
                    </tr>
                </div>                              
            </table>
        </form>

      </body>
       </html>
1
  • 1
    please format it properly Commented Mar 18, 2013 at 6:30

3 Answers 3

2

try this way

HTML

<form align="center" action="yourpage.php?func_name=updateRecord" method="post">

PHP

$form_action_func = $_POST['func_name'];

if (function_exists($form_action_func)) {
  updateRecord();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't execute arbitrary functions based on user-supplied parameters!
2

write form action="" and then write your php code as below note : use form method as get

<?php
if(isset($_GET['id']))
{
       call your function here
}
?>

in function access all values using $_GET['fieldname']

3 Comments

These are not good solutions because if you have several forms on the page, then it might just be unmanageable. It's cleaner to write code where functions are called rather than relying on code in the global scope.
As per my knowledge only one action fire for one form.- only one form submit at a time.
This is true, however probably still not the best practice because you end up with a series of if (isset($_GET['action']==submitform1) { do this } if (isset($GET[action'] == submitform2) { do that } .. which is not very elegant. It would be better to call functions directly.
0

simple way make your "Submit " and "Update" action performed on same page then

if(isset($_POST['update']))
{
 //perform update task
 update($var1,var2,$etc); // pass variables to function
 header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully 
}
else
{
// display form
}

Comments

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.