How to embed PHP into Javascript?

Discussion in 'PHP' started by chxxangie, Nov 11, 2007.

  1. #1
    is it possible?

    
       <SCRIPT LANGUAGE="Javascript">	
          document.location = "product.php?search=<?php echo $search;?>"
       </SCRIPT> 
    
    Code (markup):
     
    chxxangie, Nov 11, 2007 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    passing php variable to JS.
    try this one

    
    <script>
    function passVariable(var)
    {
        document.location = "product.php?search=var"
    }
    </script>
    <body>
    <form method="POST">
    <input type="text" id="variable" name="search">
    <input type="submit" value="click me">
    </form>
    </body>
    
    HTML:
    php....
    
    $search = $_POST['search'];
    echo "<input type=\"hidden\" id=\"variable\" value='" . $search . "' onchange="passVariable($search)">";
    
    PHP:
    hope this helps
     
    bartolay13, Nov 11, 2007 IP
  3. jonimontana

    jonimontana Well-Known Member

    Messages:
    262
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #3
    if thats what you need (i dont shur)
     
    jonimontana, Nov 11, 2007 IP
  4. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i'd tried...but the passing variable SEARCH is {$search}...
    seem there is something wrong...:confused:
     
    chxxangie, Nov 12, 2007 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    try the code ive given
     
    bartolay13, Nov 12, 2007 IP
  6. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What you have in your first post should work fine as long as the file is parsed as PHP

    You wont need to pass a variable in.
     
    tonybogs, Nov 12, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I think this is what you want (ignore the 2 above "solutions"):
    <form action="product.php" method="get">
    <input type="text" name="search" />
    <input type="submit" value="Search" />
    </form>
    Code (markup):
    When a users submits the form, they will be taken to product.php?search=<search box contents>. Also, this way, it removes the JS requirement.
     
    krt, Nov 12, 2007 IP