user3304642
user3304642

Reputation: 191

How to execute javascript without click or onload event from php?

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 ?

Upvotes: 1

Views: 1472

Answers (5)

Mark Hughes
Mark Hughes

Reputation: 438

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.

Upvotes: 1

Quentin
Quentin

Reputation: 944084

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.

Upvotes: 1

c0d3
c0d3

Reputation: 117

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

above might work for you.

Upvotes: 0

Tuan
Tuan

Reputation: 5412

Have you tried putting the arguments outside the quotes?

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

Upvotes: 0

Ali
Ali

Reputation: 3461

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.

Upvotes: 0

Related Questions