-1

I'm trying to execute a php statement if my javascript condition is true so i write this little code :

<input id="checkbox1" type="checkbox"> MSI<br></input>

<script>
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){
          if ($('#checkbox1').is(':checked')) <?php echo "Bonjour le monde";?>;

	});
});
</script>

But this don't work, if someone can help me it will be great.

2
  • 5
    PHP runs on the server side, so it will run before the html/js is server (and parsed) by the browser. If you want to conditionally run some PHP in JS you will need to send an ajax request to a server side script that may conditionally return some data. Commented Nov 20, 2016 at 14:38
  • @JimL and how to request an ajax to a server because i really don't now Commented Nov 20, 2016 at 14:41

2 Answers 2

0

Use onChange and a function

<input id="checkbox1" type="checkbox" onChange='myFunction()'> MSI<br></input>

<script>
function myFunction() {
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){
          if ($('#checkbox1').is(':checked')) <?php echo "Bonjour le monde";?>;

	});
});
}
</script>
'

Hopefully that will work

Sign up to request clarification or add additional context in comments.

Comments

0

Since PHP is evaluated on the server before the page is sent to your browser you should send a new request to the server and get a new page back with response.SO you should try something like this.

$('input[type="checkbox"]').click(function(){
     var checked = $('#checkbox1').is(':checked')
     $.ajax({
         type: 'GET',
         url: '/path/page.php',
         data: 'answer=' + answer,
         success: function(response) {
             if(response)
                 <?php echo "Bonjour le monde";?>;
         }
     });

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.