简体   繁体   中英

How to call a PHP function on HTML form submission?

I am having some trouble sending data from a form to a function

Here is my form:

echo "<form id='AddCompany' action='' method=post>";
echo "<table>";
echo "<tr>";
            echo "<th colspan='2'>Opret nyt firma</th>";
            echo "</tr>";
            echo "<td>Firma Navn:<td><input type='text' name='Name' /></td>";
            echo "</td></tr>";
            echo "<td>Adresse:<td><input type='text' name='Address' /></td>";
            echo "</td></tr>";
            echo "<td>Postnr.:<td><input type='text' name='Zipcode' /></td>";
            echo "</td></tr>";
            echo "<td>By:<td><input type='text' name='City' /></td>";
            echo "</td></tr>";
            echo "<td>Land:<td><input type='text' name='Country' /></td>";
            echo "</td></tr>";
            echo "<td>Tlf:<td><input type='text' name='Phone' /></td>";
            echo "</td></tr>";
            echo "<td>Slogan:<td><input type='text' name='Slogan' /></td>";
            echo "</td></tr>";
            echo "<td>Catch All Email:<td><input type='text' name='Email' /></td>";
            echo "</td></tr>";
            echo "<td>Logo 1:<td><input type='FILE' name='Logo1' /></td>";
            echo "</td></tr>";
            echo "<td>Lille  Logo:<td><input type='FILE' name='SmallLogo' /></td>";
            echo "</td></tr>";
            echo "<td>Logo 2:<td><input type='FILE' name='Logo2' /></td>";
            echo "</td></tr>";
            echo "<td colspan='2'><input type='submit' value='Opret Firma' />";
            echo "</form>";
            echo "</td></tr>";
            echo "</tr>";
            echo "</table>";

I want to send the data to this function

function insertRecord ($fieldarray)
    {
        $this->errors = array();

        global $dbconnect, $query;
        $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);

        $fieldlist = $this->fieldlist;
        foreach ($fieldarray as $field => $fieldvalue) {
            if (!in_array($field, $fieldlist)) {
                unset ($fieldarray[$field]);
            } // if
        } // foreach

        $query = "INSERT INTO $this->tablename SET ";
        foreach ($fieldarray as $item => $value) {
            $query .= "$item='$value', ";
        } // foreach
        $query = rtrim($query, ', ');
        return;
    } // insertRecord

I just realised you might need this bit of code also:

class Company extends Default_Table
{
    // additional class variables go here
    function __construct ()
    {
        $this->tablename       = 'Company';
        $this->dbname          = 'oop';
        $this->rows_per_page   = 15;
        $this->fieldlist       = array('ID', 'Name', 'Address','Zipcode','City','Country','Phone','Slogan','CatchAllEmail','Logo1','LogoSmall','Logo2');
        $this->fieldlist['ID'] = array('pkey' => 'y');
    } // __construct
} // Company

Where do I call the function and which variables should I send with it?

I have tried onclick=insertRecord() which doesn't seem to work, and I am thinking it is because I need to insert a variable, which I think needs to be an array of the form data as the function is declared with an array.

As you can see I am pretty much a tenderfoot at this.

I have found out the following:

If I echo my $query after the foreach loop in the insertRecord function I see the query like so: INSERT INTO Company SET Name='1', Address='2', Zipcode='3', City='4', Country='5', Phone='6', Slogan='7', CatchAllEmail='8', Logo1='', LogoSmall='', Logo2=''

However still nothing is being inserted, I have tried this on 2 servers and with users I know have insert rights however nothing is getting inserted.

No need to use function but you can do like, remove action ="" from <form> .

then check like,

    if($_POST){
     //your function logic
    // get variable data like 
      echo $_POST["Name"];
    }  

Enjoy!

Please take this in the most friendly way, I recommend that you pick up a book or read a tutorial about PHP/MySQL/HTML/JavaScript.
I'll do my best to explain this without going outside the scope of this Q&A site.

What you have in the first part of the code is an HTML form echo ed by a PHP script, so the HTML form is now in the client's browser.

What you're trying to do with onclick=insertRecord() is binding an event handler written in PHP (server side) to an event fired in browser (client side).

How to solve your problem?

You need to POST this form to the PHP script containing the insertRecord() function. First you need to modify the action attribute in your <form> and point it to the .php script that has this function, then you simply do something like this:

$cmpny = new Company();
$cmpny->insertRecord($_POST);

You need to have some way of getting your POST data into the $fieldarray. Could be something as simple as:

function insertRecord ($fieldarray) {
   //the function you have above
}

insertRecord($_POST);

Of course this is depending on how the insertRecord function is set up to work and if it will recognize all the POST data in the way you have things set up.

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