1

I am using HTML as my base and my file is called index.php - I have a secondary .php page called PHPSyntax.php that holds php functions in it. How from within index.php can I call a function from phpsyntax.php and return the result to index.html

File structure is like this: Index.php

<html>
<body>
    <h4>PHP Testing</h4>
    <form> 
        <select> 
            <option value="null" selected="selected"></option>
            //here is where I want to call and return the values from my php
    </form>
  </body>
</html>

And my PHPSyntax.php
reads like this

<?php
  $servername = "localhost";
  $username = "username";
  $password = "password";
  $dbname = "test";
  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
  $sql = "Select name As 'employeename' from employees";
  $result = $conn->query($sql);
  if ($result->num_rows > 0) 
  {
  while($row = $result->fetch_assoc()) { echo "<option value=".$row['employeename'].">".$row['employeename']."</option><br>"; }
  } 
  else echo "<option value='null'>default</option>";
  $conn->close();
?>  
1
  • Try require_once ('PHPSyntax.php')or include('PHPSyntax.php') if the two files are in the same folder. Commented Mar 28, 2016 at 0:22

3 Answers 3

2

Change the name of index.html to index.php and include the .php code like so:

<?php require_once 'PHPSyntax.php' ?>
Sign up to request clarification or add additional context in comments.

5 Comments

Where would this syntax go?
Where you have the comment.
Does not return the results.
You need to change the filename to index.php as well. Normally web servers are not set to send HTML files to PHP
@JimL - changed that one :) --- ugh, I caught my mishap I had a space between the <? php instead of <?php
0

I might be missing something - but why can't you leave the index.html as your main page and do an Ajax call to the .php page on page load and append the returned content into the desired location using the success function of hte Ajax call. That way you keep the .html extension, you set the form elements dynamically (and it appears that all you are doing is setting the employees names into the select list) and all is good in the world.

Comments

0
<?php

/*PHPSyntax.php Is not work because you have to change index.html for index.php. So, once you do that, you have to do everything in the same file unless you make functions into the your extra file that connect with your database.

  1. Method One. Do it with two separate files and make a function for display your option trough php. How? See this.*/

    function get_options() {
    
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "test";
      $conn = new mysqli($servername, $username, $password, $dbname);
      if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
      $sql = "Select name As 'employeename' from employees";
      $result = $conn->query($sql);
      if ($result->num_rows > 0) 
      {
      while($row = $result->fetch_assoc()) { 
    
    $option =  "<option value=".$row['employeename'].">".$row['employeename']."</option><br>"; 
    return $option;
    }
      } 
      else $option =  "<option value='null'>default</option>"; return $option;
      $conn->close();
    
    }  //Once you already make a function, in your index.php require the file PHPSyntax.php like
    
    ?>
    
      <form> 
        <select> 
            <option value="null" selected="selected"></option>
            <?php echo get_options(); ?>
    </form>
    

3 Comments

That's not entirely true. One can instruct their system to treat .html files as PHP.
this is missing the closing </select>, and also if I change my syntax to try this, it no longer executes correctly.
Actually, I caught the issue. In the index.php it should read <?php include PHPsyntax.php echo get_options(); ?> (that's the php syntax) it would also need the closing </select> tag. WHY does the function not require a return statement? As a side note, I actually like this approach better as I can create a utility.php and re-use multiple functions this way.

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.