-3

I was going to run krimaction1, a PH function when the JavaScript runs, but I've not had any luck solving this and I'm not really good at JS. How can I run that PHP function in the JavaScript?

Here is the code I've tried so far

<?php

function krimaction1() {
    echo"IT IS ALIVE!";
}

?>
<script>
    $(function(){
        $('#krim_1').click(function(){
            alert(this.id);
            var myTD = this.id.split('_')[1];
            var newFrm = '<form id="myNewForm1"><input name="newdata" value="' +myTD+ '" /></form>';
            $('body').append(newFrm);
            $('#myNewForm').submit();
        });
    });
</script>

I've tried another method with this as my JS:

function myFunction() {
    document.getElementById("ThisIsTheValue").value = "Johnny Bravo";
    document.getElementById("SubmitForm").click();
}

but that code doesn't work either for me, as when I try to set the value with the JS and click it it doesn't do anything and I can't really see what's wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Patric Nøis
  • 208
  • 2
  • 8
  • 27

1 Answers1

1

It seems the action of myNewForm is the php page itself, so you can add

<?PHP 
  function krimaction1() {
   echo "IT IS ALIVE!";
  }

  if (isset($_GET["newdata"])) { krimaction1(); } 
?>

to the page to have it execute when the form is submitted

I use $_GET here since that is the default for the form you have created.

If you do NOT want to reload the page to see IT IS ALIVE, you need to AJAX the result

mplungjan
  • 169,008
  • 28
  • 173
  • 236