0

I looked at this post to try and understand the object operator better: Where do we use the object operator "->" in PHP?

But when I copied and pasted the php in the 2nd response, I just get a blank page when I run it. Here is my code:

PHP:

<?php

    class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}

$a = new SimpleClass();
echo $a->var;
$a->displayVar();

?>

HTML:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>

        <form action ="process.php" method ="POST">
            First Name: <input type="text" name="fName"> 

            <input type="submit">       
        </form>

        <p>
            Click for objectOperator.php
            <button onsubmit="objectOperator.php">Submit</button>
        </p>
    </body>
</html>

Ignore the form in the HTML, that was for something else I was doing. When I click on the Submit button I want to run the php in objectOperator.php (which is the name of my php file). But when I click it nothing happens.

Community
  • 1
  • 1
  • Do you get any errors ? – Vasil Rashkov Mar 07 '16 at 04:26
  • 2
    The `onsubmit` attribute is supposed to contain Javascript. Not the name of a file. And unless I'm mistaken, it's only valid inside a `
    ` element.
    – r3mainer Mar 07 '16 at 04:26
  • 1
    First of all you can only place a JavaScript function or code in the onsubmit event handler. Second, onsubmit is not valid for a button. – purpleninja Mar 07 '16 at 04:28
  • Thanks you two, I made a new form that called the php fille with action="..." and got it working now. –  Mar 07 '16 at 04:36

1 Answers1

0

You can try this

I just tried this on phpfiddle and it was working

<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';

// method declaration
public function displayVar() {
    echo $this->var;
}
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$a = new SimpleClass();
echo $a->var;
$a->displayVar();
}
?>


<form action ="" method ="POST">First Name: <input type="text" name="fName"> 
<input type="submit">       
</form>

The key is to have the correct action

AceWebDesign
  • 579
  • 2
  • 11