1

so I'm fairly new to programming/scripting HTML.

my current project is PHP/HTML, building a website, the HTML templates are prepared but I need to insert PHP and JavaScript to make things work.

Today's issue, I'm creating a registration form for my website (before I create the login and secure pages...)

I found some useful info at w3schools allowing me to copy and modify the below code.

the code was working okay except errors were showing on the page until I fixed them.

I wanted to stack the php into a function so that undefined vars of database handling did not show on page until the function had run and vars had been defined, completely negating any errors from the other php files.

my problem now is that the function either isn't running on click or the values are not being returned to echo for debugging.

perhaps as this is my fist ever PHP page I'm missing something you guys could help with

I have checked w3schools & google for answers but nothing seems to be detailed enough, or the issue isn't the same.

(File Names have been changed and junk such as classes & styles have been removed for readability, external files not included nor required)

//PHP>Register page> php placed before form html
<?php
    //define variables
        //error var
    $usernameErr = $emailErr = $passwordErr = $repeatpassErr = "";
        //transferable vars
    $username = $email = $password = $repeatpass = "";

    //create session manager
        function session_reg(){
        //Start session registration
        session_start();
        //require push file
        require 'secure-sql.php';
        //confirm request method
            if($_SERVER['REQUEST_METHOD'] == "POST"){
            //confirm fields are populated
            if(empty($_POST["username"])) {
                    //if empty error
            $usernameErr = "Name is required";
            }else {
            //else confirm input and check restrictions
                $username = test_input($_POST["username"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
                        $usernameErr = "Only letters and white space allowed";
                }
            }
            //confirm fields are populated
            if(empty($_POST["email"])) {
                $emailErr = "Email is required";
            }else {
                $email = test_input($_POST["email"]);
                // check if e-mail address is well-formed
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $emailErr = "Invalid email format"; 
                }
            }
            //confirm fields are populated
            if(empty($_POST["password"])){
                $passwordErr = "Must enter a password";
            }else{
                $password = test_input($_POST["password"]);
            }
            //confirm fields are populated
            if(empty($_POST["repeatpass"])){
                $repeatpassErr = "Must repeat your password";
            }else{                      
                $repeatpass = test_input($_POST["repeatpass"]);
            }
        }
        //compare password and repeat are the same
        if ($password === $repeatpass){
            //db_quote to secure from injection
            $username = db_quote($_POST['username']);
            $email = db_quote($_POST['email']);
            $password = db_quote($_POST['password']);
//section is a work in progress, shouldn't be required for local debugging
            //query db for usernames and emails to avoid dupes
            //db_query()
            //insert into db
        }else{
            //or return password error
            $passwordErr = "passwords must be the same";
        }
    }
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>

I attempted to use $GLOBALS['varname'] to call the variables from outside the function and return $varname to push it back for echo, but I got no results, no errors.

here is the HTML form so that you know how I'm trying to move vars around.

//PHP> Register Page>After PHP Functions
<h2>Register</h2>
<h6>* Required field<h6>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
        <table class='reg-table' style='align-content:center;'>
            <tr>
                <td>Username:</td>
                <td><input size='20' type="text" name="username" value='<?php echo $username;?>'><span>* <?php echo $usernameErr;?></span></td>
            </tr>
            <tr>
                <td>E-mail:</td> 
                <td><input type="text" name="email" value='<?php echo $email;?>'><span>* <?php echo $emailErr;?></span></td>
            </tr>
            <tr>
                <td>Password:</td>
            <td><input pattern=".{5,}" required title="5 characters minimum" type="password" name="password" ><span>* <?php echo $passwordErr;?></span></td>
            </tr>
            <tr>
                <td>Repeat Password:</td>
                <td><input type="password" name="repeatpass"><span>* <?php echo $passwordErr;?></span></td>
            </tr>
            <tr>
                <td></td>
            <td class="g-recaptcha" required></tr>
            </tr>
            <tr>
                <td></td>
                <td><input onclick="session_reg()" type="submit" name="submit" value="Register"></td>
            </tr>
        </table>
    </form>

Finally I'm trying to debug the outputs onto the same page I'm working with. once I'm finished fixing the breaks this section will be commented out or removed.

//debug output
    echo "<h3>var outputs</h3>";
    echo "<br>";
    echo $username;
    echo "<br>";
    echo $usernameErr;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $emailErr;
    echo "<br>";
    echo $password;
    echo "<br>";
    echo $passwordErr;
    echo "<br>";
    echo "<h3>end debug</h3>";
    ?>
L.H.
  • 27
  • 9
  • Possible duplicate of [Call php function from javascript](https://stackoverflow.com/questions/7165395/call-php-function-from-javascript) –  May 29 '17 at 12:05
  • Thank you for the input, this should help me moving forward – L.H. May 29 '17 at 12:16

1 Answers1

0

It seems to me you are trying to load a PHP function with onclick. You can only call JavaScript functions with onclick.

<td><input onclick="session_reg()" type="submit" name="submit" value="Register"></td>
andreini
  • 188
  • 1
  • 3
  • 17
  • ok - my mistake, so, how would i allow the function to run only on click in PHP - or should i make the function a JS instead and only have the `required` errors in PHP? – L.H. May 29 '17 at 11:53
  • You can use an Ajax request if you don't want to refresh the page: onclick triggeres the Ajax request -> which loads and executes the PHP function -> the PHP response is sent back to JavaScript. Or yes, you can make the check with JavaScript on the same page instead of PHP, however this is less secure. Also, please mark my answer as correct if it helped you :) – andreini May 29 '17 at 12:08
  • 1
    this does actually help me solve a few other issues that i didn't want to tackle till later, thank you – L.H. May 29 '17 at 12:15