1

I have one javascript named func.js, in that there is one function called show which takes 2 arguments, what I need to do is I want to call that function from php, I can't use any click or onload event here my script looks like this

   <html>
   <head></head>
   <script type='text/javascript' src='path/to/func.js'></script>
   <body>
        some div etc
   <form method='post' action="" >
       .....
       .....
   </form>
   </body>
   </html>

   <!-- after submit of form validation is in php -->
   <?php

       /* here I want to call javascript, where arguments are php variables 
            show('argument1','argument2'); */

       // I tried to echo like this
        echo "<script>show('$argument1',$argument2')</script>";
   ?>

So what's the solution for my case ?

1
  • You could use AJAX, and validate the data on a different PHP file, and use the success part to run the script. Commented Mar 15, 2014 at 7:37

5 Answers 5

1

The code you have should work… most of the time. Unfortunately, you haven't told us why it doesn't work - is there a PHP error? Is there a JS error? — and you haven't shown us either the resulting JavaScript that PHP is outputting or the contents of the variables so we can figure it out for ourselves.

The two most likely explanations (and the only ones that occur to me at the moment) for the problem are:

There is a problem with the data in the variables

That the variables contain characters which cannot appear inside JavaScript strings or ' characters which must be escaped inside JavaScript strings.

JSON is a sufficient subset of JavaScript that the json_encode function will escape (and quote) most data so it is suitable for use in JS.

<script>
show(<?php echo json_encode($argument1); ?>, <?php echo json_encode($argument2); ?>)
</script>

There is a problem with your timing

You have an HTML comment saying "after submit of form validation is in php", but there is nothing in the code you have shared to enforce that.

You need to have something like if (isset($_POST['some_data_from_your_form'])) { ... } wrapped around the generation of the script so it only appears when the form is submitted and not when it initially loads.


If that doesn't work, then you really do need to look at what the variables are, what the generated JS is, and what your JavaScript error console says.


Script elements are not allowed after the end of the HTML element. While browsers will recover from that error, you really should move the script inside the BODY.

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

1 Comment

Thank you so much for your time json_encode worked like a charm +1 and accepted... thanks a lot @Quentin
1

It could be to do with the data inside the arguments, what sort of data is it?

echo "<script>show('".str_replace("'", "\'", $argument1)."', '".str_replace("'", "\'", $argument2)."')</script>";

If you're passing information such as J'min it will cause an issue. Does the data have multiple lines? Then it needs to be filtered.

Comments

0

First of all, your tags are broken

<script type='text/javascript' href='path/to/func.js'</script>

You should change href to src and close the script tag, so it becomes

<script type='text/javascript' src='path/to/func.js'>

Also, javascript is client-sided which means you can't call javascript functions in PHP.

I think a good solution here would be to use an AJAX call to validate your form.

1 Comment

Thanks for reply, unfortunately not working..can you give demo ?
0

Have you tried putting the arguments outside the quotes?

echo "<script>show('".$argument1."', '".$argument2."')</script>";

2 Comments

He's using double quotes to put the variables inside the echo statement, this should not be a problem.
Thanks for reply, unfortunately not working..can you give demo ?
0
echo '<script type="text/javascript">show(' . $argument1 . ',' . $argument2 . ');</script>';

above might work for you.

3 Comments

Thanks for reply, unfortunately not working..can you give demo ?
echo '<script type="text/javascript">show(' .$arg1. ',' .$arg2. '); </script>'; worked when i tested.
. is not needed while using single quote anyways thanks for reply +1

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.