0

I'm trying to call a JavaScript function from my PHP output.

The problem is that the function does not fire.

Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="stylesheet" type="text/css" href="styles.css" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="build_workout_functions.js" charset="UTF-8"></script>
    </head>
<?php

  if ( isset( $_GET[id] ) ) {

  echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script src="build_workout_functions.js" charset="UTF-8">
        getUserProgram('.$_GET[id].');
    </script>';
  }

?>
    <body id="login-bg"> 

        <div class="workout_table">

            <select name="days">
                <option value="A">A</option>
                <option value="B">B</option>
                <option value="C">C</option>
                <option value="D">D</option>
                <option value="E">E</option>
                <option value="F">F</option>
            </select>

            <select name="muscles" onchange="getExercies()">
                <option value="legs">legs</option>
                <option value="back">back</option>
            </select>

            <select name="exericse"><select>

        </div>

    </body>
</html>

The JavaScript code:

function getUserProgram ( userId ) {

    alert(userId);

    $.ajax({
        type: "POST",
        url: "get_user_program.php",
        data: {id: userId}
    })
    .done(function( msg ) {
        alert( "Data Saved: " + msg );
    });

}
1
  • 1
    You should not mix "src" attribute AND code inside of a <script> tag. Commented Mar 3, 2014 at 14:16

2 Answers 2

4

A few things:

  • Script tags should be either in the <head> or the <body> (yours is not)
  • You're already loading jQuery and build_workout_functions.js in your head. Your PHP output doesn't need to reload them.
  • You should not put content in a <script> tag that has a src attribute.

Consider the documentation here:

If there is a src attribute, the element must be either empty or contain only script documentation that also matches script content restrictions.

What this means is that you should only provided JavaScript commented content when using the src attribute. An example of this is provided for us on the w3c website:

<script src="cool-effects.js">
 // create new instances using:
 //    var e = new Effect();
 // start the effect using .play, stop using .stop:
 //    e.play();
 //    e.stop();
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

These are the "must be learnt" things before considering other answers.
0

Just write as html format, if if condition is met, then your script will be written and so executed, otherwise not.

Note: I assumed that you wrote first two lines of <script> as trials and you call getUserProgram() as trial, too.

<?php

if(isset($_GET[id]))
{
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="build_workout_functions.js" charset="UTF-8">
    getUserProgram('<?php echo $_GET[id]; ?>');
</script>

<?php
}

?>

I also echo $_GET[id] in the function call and I left $_GET[id] as it is, it can be wrong by the way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.