1

I'm working on a project at the moment and was trying to implement a form which submitted on the same page by running a php function on the "submit" button press which actually just runs the function. However, I was having a bit of difficulty when testing this and it seemed unresponsive with tests of the PHP before and after it.

Here's my code:

<?php
function lcontactSend() {
    if(isset($_POST['lcontactname'])){
    echo $_POST['lcontactname'];
    } else {
        echo "Test Success";
    }

}
?>

<div style="color: #818181; width: auto; height 50vh; display:flex; flex-direction: row; justify-content: space-around;">

<div style="padding: 2vw; width: 35vw; height: 50vh; display: flex; justify-content: top; flex-direction: column; ">
<h2 style="color: #818181; margin: 0;">Contact</h2>
Questions? Go ahead.
<form method="post">
<p><input name='lcontactname' type="text" placeholder="Name" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactemail' type="text" placeholder="Email" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactsubject' type="text" placeholder="Subject" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactmessage' type="text" placeholder="Message" style="height: 5vh; width:28vw" required></p>
<p><input type='button' value='send' style="height: 5vh; width: 28vw; color: white;background-color:black; border: 2px white; padding: auto; font-size: calc(1vw + 0.5vh);" onClick='lcontactSend();'></p>
</form>
</div>

Does anybody have suggestions for where to head next? I was ideally hoping to stray away from refreshing the page of using as that is not compatible with some browsers.

Edit: Thank you for explaining the difference between client-side and server-side code. Still though, any ideas for how to apply that here because to me it seems a JavaScript way has to refresh the page or just won't work?

:)

  • 1
    `onClick` will look for a js function not a server side function – Masivuye Cokile Oct 26 '18 at 12:33
  • 1
    PHP runs on the server, once. The result is passed to the browser and displayed. Then JavaScript starts to run. If you want to run PHP code without leaving the page, you need to learn AJAX. –  Oct 26 '18 at 12:38

0 Answers0