1

I have a small problem loading a php function if(isset... to a page where is form with onsubmit function :

function functionlol() {

$('#phptestlol').load('system/phptest.php');

}

Now, the phptest.php contains this :

if(isset($_POST['test'])){
echo "Working with submission also";
}

echo "Working";

and html with form looks like this:

<span id='phptestlol'></span>

<form method='POST' onsubmit='functionlol()' target='test'>
<input type='submit' name='test' value='Submit'>
</form>

I did target='test' because I don't want page to be refreshed. When I click the submit button, it loads the php file, but it shows only the "Working" echo, and doesn't echo that "Working with submission also"... what am i supposed to do to make it work? Thanks!

EDIT WHOLE HTML:

<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>


<script>

$('form').submit(function(event) {
    event.preventDefault();
    $('#phptestlol').load('system/phptest.php', {test:1});
});

</script>

</head>
<body>
<iframe name='test' style='display:none;'></iframe>

<span id='phptestlol'></span>

<form method='POST' target='test'>
<input type='submit' name='test' value='Submit'>
</form>
</body>
</html>

PHP (phptest.php):

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
if(isset($_POST['test'])){
    echo "submitted";
}
?>

1 Answers1

2

Let's make some adjustments and take advantage of jQuery all the way through. First the HTML -

<span id='phptestlol'></span>

<form method='POST' target='test'>
    <input type='submit' name='test' value='Submit'>
</form> 

Note the removal of any inline JavaScript. Now for the PHP -

if(isset($_POST['test'])){
    echo "submitted";
}

Now for the cherry on top of the cake, the jQuery

$(document).ready(function() {
    $('form').submit(function(event) {
        event.preventDefault(); // prevents the default action of the submit button
        $('#phptestlol').load('system/phptest.php', {test:1});
    });
});

Here we prevent the default action of clicking the submit button. We also send a value for the $_POST array ({test:1}) so the isset($_POST['test']) will work as we expect.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • So in this case, there is no need to add onsubmit function? But then, all forms submitted would look the same right? or that $('form') is the name of the form or something like that? – Peťko Tomko Jun 16 '15 at 17:53
  • That is correct, no `onsubmit()` is needed. `$('form')` is the only form we have right now, but you could use a class, name or id for any specific form you have. – Jay Blanchard Jun 16 '15 at 17:55
  • Oh, so it's that simple! So if there would be `form class='test'`, then js will be `$('.test')` right? As a beginner with js, I thought it will be much harder to do this with jQuey, AJAX etc.., but I'm surprised...It's working perfectly! – Peťko Tomko Jun 16 '15 at 17:59
  • A form with class test would be `$('.test')`, but yes, it is *just* that simple. – Jay Blanchard Jun 16 '15 at 18:00
  • Glad to help @PeťkoTomko – Jay Blanchard Jun 16 '15 at 18:02
  • One more thing if you send request `$('#phptestlol').load('system/phptest.php', "test=1" );` you will get value of **test** in $_GET Parameter because you use string instead of Javascript object – jagad89 Jun 16 '15 at 18:07
  • uhm, idk why but now, doesn't work even that accepted solution...I am pretty sure there's exactly everything the same in my codes but just rewrited that typo (sumbmit =submit) and {test:1} = {'test':1}... any suggestion what could be wrong? – Peťko Tomko Jun 16 '15 at 18:21
  • Add error reporting to the top of your PHP file(s) right after your opening ` – Jay Blanchard Jun 16 '15 at 18:51
  • Remove the quotes from {test:1} – Jay Blanchard Jun 16 '15 at 18:53
  • @JayBlanchard - Same..:( – Peťko Tomko Jun 16 '15 at 18:58
  • Hmmmm....I don't understand how it could be working one minute and not the next. Edit your original post and add your updated code. – Jay Blanchard Jun 16 '15 at 18:59
  • You need to wrap your jQuery code in a document ready handler - `$(document).ready(function() { ....//your code here....});` See my updated answer @PeťkoTomko – Jay Blanchard Jun 16 '15 at 19:04
  • 1
    @JayBlanchard - Works! :3 Thank you very much! – Peťko Tomko Jun 16 '15 at 19:07