2

I am passing a value to a function. In the function, I use PHP code to accomplish my task. I need to imbed the value passed into the function into the PHP string.

Here is the function:

<script type="text/javascript">
    function getData(p1) {
        alert("Variable is: " + p1);
        <?php
        $club=window.title.value;
        $username="user";
        $password="password";
        $database="database";
        $datahost="example.db.4729287.hostedresource.com";

        mysql_connect($datahost,$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        $query="SELECT * FROM golfinfo WHERE clubname = 'Mirabel'";
        $result=mysql_query($query);
        $row = mysql_fetch_array($result);
            ?> alert("row is: " + p1); 
            window.ClubName.value= "<?php echo $row['ClubName'] ?>";
            window.Privacy.value = "<?php echo $row['Privacy'] ?>";
            window.Membership.value = "<?php echo $row['Membership'] ?>";
            window.Type.value = "<?php echo $row['Type'] ?>";
    }

</script>

This function is called as follows:

I would like to replace the: WHERE clubname = 'Mirabel' with the parameter p1 like: WHERE clubname = p1

I am at a loss to find a way to imbed the p1 into the string.

Any suggestions? Thanks

6
  • 1
    Since you seem to be learning, a couple of tips: 1. mysql_connect and other mysql_ functions are deprecated as their documentation clearly states. Better use an alternative such as PDO. 2. This is not just about embedding a variable in a PHP string, but about calling a piece of PHP from JavaScript. This isn't as easy as you make it. You can't mix JavaScript and PHP like this at all. PHP runs on the server, JavaScript on the client, and you can't just mix them. Commented Aug 2, 2015 at 22:20
  • 1
    If you want to use PHP after the page has already loaded, you need to use AJAX. Google around for a tutorial, there are many. Commented Aug 2, 2015 at 22:24
  • You cannot send data from JavaScript to PHP in this way. The PHP is executed before the JavaScript even hits the page. As @rjdown said, you would need to send the p1 value through AJAX to your PHP script, then have the script return the information back for processing the response in your JavaScript funtion. Commented Aug 2, 2015 at 22:27
  • Thanks to all of you who helped explain these details to me. I will take these suggestions and rework my whole program. Commented Aug 3, 2015 at 1:17
  • When you do so, make sure also to change your password. I've changed it in your question now, but some people will still be able to read it from an earlier version. Commented Aug 3, 2015 at 7:50

1 Answer 1

1

It cannot be done.

You are building a javascript function with a bit of php code on the server. The PHP will process the code on the server and then send it to the browser on the client computer.

The javascript will be triggered on the client computer having already been sent from the server. You cannot pass variables into the javascript function (which is occuring on the client computer) and expect it to be processed by PHP which is on the server.

I would suggest you investigate sending the variable to a php script on the server via an ajax call.

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

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.