0

I would like to create a php IF statement so If it receives the key "test" by POST, <h2> hello </ h2> should be output, otherwise <span> error </ span> How can I make that?

<div id="rre">
  <h2 class="desktop">Hamburg,a major port city in northern Germany.</h2>
  <h2 class="mobil">Berlin,is the capital and the largest city of Germany.</h2>
</div>
<form action="viereck.html" method="POST">
    <input type="text" name="check"?>
    <input type="button" value="Klick"  onclick="klick();" />
    </form>

     <script type="text/javascript">
           function klick() {  
           jQuery('#rre').html('<h1>Hallo</h1>');         
          }           
       </script>
Jefferson
  • 794
  • 10
  • 24
MOS
  • 31
  • 1
  • 9
  • 1
    PHP doesn't run Javascript. PHP runs on the server. Javascript, outside of node, runs on the client. The best you can do is write your php to conditionally create the javascript that will execute what you want depending upon the inputs PHP received. But PHP will not execute that javascript. The client will when they receive your payload. – Taplar Aug 21 '17 at 14:52
  • 1
    @Taplar I think he just wants to pass a value to the front end, but he isn't very clear about it – Shardj Aug 21 '17 at 14:55
  • I would like to do such a thing if ($_POST["check"] =="test") echo "jQuery('#rre').html('

    Hallo

    ')"; else{ echo " error span>"; }
    – MOS Aug 21 '17 at 15:03

3 Answers3

0

The response that you return needs to be inside script tags like:

echo '<script type="text/javascript">alert();</script>';
DenizEng
  • 400
  • 4
  • 14
0

Why you don't do that with jquery: Try smth like this

<script type="text/javascript">
    function klick() {  
        if($("#text").val() == 'test'){
           jQuery('#rre').html("<h1>Hello there!</h1>");
        }else{
           jQuery('#rre').html("Error");              
        }
    }           
</script>

Html

<div id="rre">
    <h2 class="desktop">Hamburg,a major port city in northern Germany.</h2>
    <h2 class="mobil">Berlin,is the capital and the largest city of Germany.</h2>
</div>
<form action="viereck.html" method="POST">
    <input type="text" name="check"? id="text">
    <input type="button" value="Klick"  onclick="klick();" />
</form>

JSFIDDLE

Mr Alb
  • 291
  • 2
  • 4
  • 16
  • It works this way but i would like to create the condition using php, if it is possible! – MOS Aug 21 '17 at 15:31
0

If the page you're posting to is viereck.html, you're going to be unable to use PHP on it.

If you can change the file to viereck.php, you can POST to it like you're doing, with action="viereck.php" method="POST" Then, at the top of your page, check for POST values in PHP like:

<?php

if(isset($_POST['check'])){
     $check = '<span>error</span>';
     if($_POST['check'] == 'test'){
          $check = '<h1>Hallo</h1>';
     }
}

?>

And if your jQuery is on the same page, you can insert the PHP like:

<script type="text/javascript">
   function klick() {  
       jQuery('#rre').html(<?= $check; ?>);         
   }           
</script>

Doing it this way though can lead to some serious headaches as your project grows, I wouldn't recommend it. Why not look into using AJAX? You can submit a form and return a message indicating success or failure, as well as why. You can then create your HTML in jQuery instead of a PHP string like:

// On form submit
 $.ajax({
     url: "viereck.php",
     type: 'POST',
     success: function(result){
        var msg = $('<h1>').text('Hallo');
        $('#rre').html(msg);
     },
     error: function(result){
        var msg = $('<span>').text('Error');
        $('#rre').html(msg);
     }
});
Jeramiah Harland
  • 854
  • 7
  • 15