2

I have a HTML form and I want to use the results to filter the data available in a SQL file. I'm trying to connect to a local MySQL server but It doesn't work, i don't achieve to enter in the table() function.

I use the following code:

<form id="form_id" action="food_values.php" method="post" name="myform">
...
  <input onclick="table" type="button" value="Submit">

</form>

The php file contains the following code:

<html>
<body>
<?php
function table(){
    echo ("INSIDE");
    $con = mysql_connect("localhost","root"," ");
     if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    else {
        echo "CONNECTED";
    }
}
?>
</body>
</html>

It doesn't show anything... Do you know where is the error?

  • 1
    You are treating the PHP as if it were javascript, which it is not. See the following for an example on how to properly submit a form to a PHP script: http://php.net/manual/en/tutorial.forms.php. – Jim Wright Jun 05 '18 at 12:47
  • you have _defined_ the function, but didn't _call_ it. In this case you need to put `table();` after the whole `function table(){... } ` thing – cypherabe Jun 05 '18 at 12:48
  • Please modify you code both html and php. A lot of things are out of place. What is onclick doing there? – The Oracle Jun 05 '18 at 12:54
  • @cypherabe It is unclear if these are separate files or not, and I realised after what you meant so I deleted my comment. – Jim Wright Jun 05 '18 at 12:55
  • Please don't mix `mysql` (which is deprecated!) and `mysqli` – Nico Haase Jun 05 '18 at 12:56
  • ***IMPORTANT***: [Please read this topic](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Do **NOT** use `mysql_` functions. – Martin Jun 05 '18 at 12:58

2 Answers2

0

You can not execute a PHP-Function with "onclick". Execute PHP function with onClick

With a form, you load the whole .php-file if you click on the submit.

The following code should work for you:

<html>
<body>
<?php
    echo ("INSIDE");
    $con = mysql_connect("localhost","root"," ");
     if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    else {
        echo "CONNECTED";
    }
?>
</body>
</html>

And the HTML

<form id="form_id" action="food_values.php" method="post" name="myform">
...
  <input type="submit" value="Submit">

</form>
0

Change

<input onclick="table" type="button" value="Submit">

into

<input type="submit" name="submit" value="Submit"> 

As the action in form takes care of where to direct the data in php.

and in 'food_values.php' page you dont have to define function as its not javascript.

   <?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

This will connect the database. And for validation of the form follow the SQL commands. For futher detail visit https://www.w3schools.com/php/php_form_validation.asp

Suraj Libi
  • 515
  • 2
  • 9