-1

what is wrong with my code? He doesn't do the alert 'PHPPPPP' Only the success one

Html: Add to Log

          <script>
            function create () {
              $.ajax({
                  url:"log.php",    //the page containing php script
                  type: "post",    //request type,
                  data: {hour: "1", min: "2", email: "3"},
                  success:function(result){
                     alert("passed");
                  }, error: function(data) {
                      alert(data);
                  }
              });
          }
        </script>

Php(log.php):

echo "<script> alert('PHPPPPPPPPPP') </script> " ;
Muax
  • 19
  • 4
  • 1
    Well, it's not doing anything with the data it gets back (`result`). – MinusFour Mar 01 '21 at 22:48
  • @MinusFour but it isn't supossed to alert anyway? – Muax Mar 01 '21 at 22:50
  • While it's not strictly a duplicate, having a read of the answers to [this question](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) may be beneficial to you in helping to understand what is/isn't happening. – Robin Zigmond Mar 01 '21 at 22:50
  • @Muax - you'll only see an alert when that Javascript code is executed by a browser, but that won't happen on its own when you request via Ajax. You get some data back, that happens to be HTML containing a JS script, but the browser won't know to execute that JS. – Robin Zigmond Mar 01 '21 at 22:52
  • 1
    `but it isn't supossed to alert anyway?`...which alert? It should run `alert("passed");` if the request didn't fail. It won't run the script you echoed from PHP - that's stored in `result` but you're ignoring it. And it makes no sense to return actual JS from PHP in the response to an AJAX request. Just return some data, and write JS in your actual JS area to run whatever you need. Separation of concerns, and all that... – ADyson Mar 01 '21 at 22:52
  • Ohhhh but if i do something like insert on a sql table, is the php file able to do using ajax? I think im not doing the ajax thing right @RobinZigmond – Muax Mar 01 '21 at 22:54
  • the PHP script, which runs on the server, can do anything it wants. But if you want to do anything with what it `echo`s back in response, it's the responsibility of the Javascript code that makes the Ajax request to deal with that. It's not the same as the user loading a page "normally" in the browser and having the HTML response (including any embedded CSS/JS/etc) handled automatically by the browser. – Robin Zigmond Mar 01 '21 at 23:00
  • well i have this code on log.php and it doesn't work but if i delete the log.php the alert fail open up : – Muax Mar 01 '21 at 23:05
  • `but if i do something like insert on a sql table, is the php file able to do using ajax?`...yes absolutely. That has nothing to do with JS or AJAX, or how you process the response etc. It's just standard PHP. PHP can execute that, no matter which way you send the request to it. – ADyson Mar 01 '21 at 23:12
  • `header('Location: indexM.php'); exit();` won't work as you expect in an AJAX context. AJAX calls don't follow Location redirect headers. You need a different mechanism for rejecting unauthenticated requests when it's an AJAX call - e.g. simply returning a 403 status. P.S. `doesn't work` isn't a useful description of your problem. Give details of error messages or the precise unexpected behaviour. If you don't have that information to give us, then do some debugging in order to find it. – ADyson Mar 01 '21 at 23:13

1 Answers1

0

You want to add the script to your document. Since you are using jQuery:

      <script>
        function create () {
          $.ajax({
              url:"log.php",    //the page containing php script
              type: "post",    //request type,
              data: {hour: "1", min: "2", email: "3"},
              success:function(result){
                 alert("passed");
                 $('body').append(result);
              }, error: function(data) {
                  alert(data);
              }
          });
      }
    </script>

This will simply add the script to the body element. It would be better if you just omit the HTML and instead just echo the string so you could alert the result instead of adding a script to your document.

MinusFour
  • 13,913
  • 3
  • 30
  • 39