Web Analytics Made Easy -
StatCounter Linking PHP to JavaScript - CodingForum

Announcement

Collapse
No announcement yet.

Linking PHP to JavaScript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linking PHP to JavaScript

    I have a question. If I wanting to create a pop up javascript form - but pre-populate some of the input boxes with php variable... how do I go about that exactly?

    Code:
    $query = 'SELECT * FROM employees';
    $results = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array( $results )) {
    
    echo $row["name"].'<br />';
    echo $row["job_role"].'<br />';
    echo '<a href="/">EDIT EMPLOYEE DETAILS</a>'; }
    So for every row that the query produces I want to be able to have the option of editing the details for each individual employee by way of a JavaScript pop up form.

    It will need to pre-populate with the following info...

    Code:
    <form action="edit.php">
      <input name="name" type="text" value="'.$row["name"].'" /><br />
      <input name="name" type="text" value="'.$row["job_role"].'" /><br />
      <input name="name" type="text" value="'.$row["salary"].'" /><br />
      <input name="submit" type="button" value="Save Details" /><br />
    </form>
    I know how to program to edit.php and the form itself... i just don't know how to carry over the row info to the pop up form. Can someone help me?

    Cheers!

  • #2
    Syntax might not be correct but this should be close.

    PHP Code:
    <input name="name" type="text" value="<?php echo .$row["name"]; ?>" /><br />
    You also must choose a form method such as post or get, usually post. And your input name must be unique for each field.





    --
    Last edited by Len Whistler; Apr 3, 2009, 08:34 PM.
    Leonard Whistler

    Comment


    • #3
      @Len Whistler

      No dot in echo.....
      PHP Code:
      <input name="name" type="text" value="<?php echo $row["name"]; ?>" /><br />

      Comment


      • #4
        apologies, did make a few mess up there with my syntax so probably was misunderstood.

        If we have the above... and then we link to a pop up window

        php page
        Code:
        <a href="mywin.htm" target="name"
        onclick="window.open('mywin.htm','name','height=255,
        width=250,toolbar=no,directories=no,status=no,
        menubar=no,scrollbars=no,resizable=no'); return false;"
        >click here</a>
        how exactly can i then, once "mywin.htm" - the java window - is open, call variables from the parent page with the link in it so that I can pre-populate the input values.

        can i define them in the link somehow. on the htm page code if i just use <? $row["name"]; ?> it doesn't seem to have any value anymore.

        hope that explains it a bit better.

        Comment


        • #5
          You can use the GET method to pass variables to the next page through the URL or use PHP sessions. I think sessions might be better.



          -
          --------------------
          Leonard Whistler

          Comment


          • #6
            Look since other people have already given the code here I'll explain client side vs server side


            A server side language (in this case php), executes the code and only sends the output of the code to the browser, i.e the browser has nothing to do with php it doesn't even know what php is.

            A client side language (in this case JavaScript) in executed completely on the browser side, another words it executes on the viewers computer, not on the server.

            Now what your talking about is basic... you use php to generate JavaScript which is then sent to the users browser.


            for example here's the following code

            PHP Code:
            <script type="text/javascript">
            document.write("<?php $i 1$b 2$a $i $b; echo  $a?>");
            </script>
            all the browser will see is the following
            PHP Code:
            <script type="text/javascript">
            document.write("3");
            </
            script
            your using PHP to generate the javascript data

            also to answer your other question, thats basic php using get

            say you want to send data from one page to another for example


            lets say you gather the user data 51 on one page and you want to send that to another page for gathering

            Code:
            <a href="link/to/page.html?code=51">click here</a>
            in the above note the ?, that denotes that your going to send data along with the link in this case we're sending the data 51 packed into a package called code.

            you can access this data from the receiving page but using whats known as get, let's proceed

            PHP Code:
            <?php
            $ourData 
            $_GET['code']; /*here we're just getting the data that's in "code" and giving it the identifier of $ourData, for examples sake let's echo that out to the browser*/

            echo "$ourData";
            ?>
            in that case we just echoed out our data, but we could of easily just put it in a form value such as

            PHP Code:
            <?php
            $ourData 
            $_GET['code'];
            ?>
            <form>
            <input type="text" value="<?php echo "$ourData"?>" />
            </form>
            in case you don't know php... all code echoed inside of the php tags <?php ?> is considered php code which is executed before the data is sent to the browser, and echo is a command that sends data data to the browser

            Originally posted by Len Whistler View Post
            You can use the GET method to pass variables to the next page through the URL or use PHP sessions. I think sessions might be better.
            you only need to use sessions if your want to make that data available throughout an entire visited stay, such as making sure a user is logged in to view data, otherwise you'd have to log them in on every page or send data with every page using get or cookies, which can get confusing with a lot of data, let alone insecure, but just for stickying form data, $_GET is more than sufficient
            Last edited by primefalcon; Apr 4, 2009, 04:16 AM.
            get up to 32 gigaytes free cloud storage at Dropbox

            For Tips on Runescape, Visit Marlaine's Musings

            Comment

            Working...
            X