Correct way to add another javascript to PHP form?

0

Trying to implement a possible solution to my previous entry I came across a code that (they say), works for what I want, but I mess with PHP and I do not know how to add another javascript to the php form that I already have (and has a js already stuck).

<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
if( $_SERVER['REQUEST_METHOD'] == 'POST'){
echo '<script type="text/javascript">
window.location.assign("pagina.html");
</script>';
// etc
// etc
?>

And I also want to add this one:

<script type= "text/javascript">
function capitAll(e){   
    e= window.event? event.srcElement: e.target;
    var str= e.value.toLowerCase();
    var Rx= /\b([a-z]+)\b/ig;
    str= str.replace(Rx,function(w){
        return w.charAt(0).toUpperCase()+w.substring(1);
    });
    return str;
}
</script>

But I can not make it work. The form is sent (no errors), but I know something I am doing wrong ...

How is it done to add a new script to the php ...?

Thank you

----------

    
asked by 20.04.2017 в 05:09
source

1 answer

0

Ok ... a PHP file is a mixture of HTML and PHP, right? (at least in its original form) ..

You only have to put the <script> in the same way as in any other html file, making sure you place it in the right place or desado (a script can go in different places) and produce a valid HTML.

<?php
// haces muchas cosas php

// en algun punto esta tu HTML, incluido o enlinea, pero esta (no me refiero a un servio REST o similar) 
?>
<html>
  <head>
    <!-- aqui tus metas y scripts y links  -->
    <!-- asi agregas el JS, igual que en cualqueir otro html -->
    <script src="/ruta/al/archivo.js"></script>
    <script type="text/javascript">
       // esta es la opción que creo quieres. 
       window.location.assign("pagina.html");
    </script>

    <?  // si quires puedes usar PHP inline, interpolar variables, fors, etc.

    echo '<script src="/ruta/al/archivo.js"></script>';

    ?>
  </head>
  <body> 
   <!-- aqui tu pagina -->
  </body>
</html>
    
answered by 20.04.2017 в 05:26