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?

6
  • 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: php.net/manual/en/tutorial.forms.php. Commented Jun 5, 2018 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 Commented Jun 5, 2018 at 12:48
  • Please modify you code both html and php. A lot of things are out of place. What is onclick doing there? Commented Jun 5, 2018 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. Commented Jun 5, 2018 at 12:55
  • Please don't mix mysql (which is deprecated!) and mysqli Commented Jun 5, 2018 at 12:56

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

There was a function definition, not a class.

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.