简体   繁体   中英

Passing a php variable to a javascript function on button click

I am displaying a dynamic table. The problem is when I am passing the php variable to the javascript function it is showing an error Unterminate string literal. I am new to php so I dont know how we pass the variable.

       <?php
        include 'dbConf.php';
        session_start();
       if($_SERVER["REQUEST_METHOD"] == "POST"){
        if(isset($_POST["submit"]))
          {    

            $username=$_SESSION["username"];
            $productName=$_POST["productName"];
            $quantity=$_POST["quantityRequired"];
            $row = mysqli_query($conn,"INSERT into TR_Order_Details_Dummy values('$username','$productName','$quantity')");
            if(!$row) {
               die('Could not enter data: ' . mysqli_error($conn));
            }
            else{
            $result=mysqli_query($conn,"SELECT * from Order_Details where Username='".$username."' ");
            }
           }
           else
           {
            echo "dummy";
           }
      }

   ?>
<html>
   <head>
       <title>Order Details Page</title>
       <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

   </head>
 <body>
      <table id="table1">
        <th>
           <tr></tr>
           <tr>Username</tr>
           <tr>Product Name</tr>
           <tr>Quantity</tr>
        </th>
        <?php
       while($res = mysqli_fetch_array($result)) {         
            echo "<tr>";
            echo "<td>".$res['Username']."</td>";
            echo "<td>".$res['ProductName']."</td>";
            echo "<td>".$res['Quantity']."</td>";  
            $username=$res['Username'];  
            echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('\".$username.\"')\"> <a href=\"delete.php\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
        }
        ?>
      </table>
   </form> 
    <script>
      function addItem()
      {
        window.location.href="order.php";
      }
      function editItem(username)
      {
        $.ajax({
          type:"POST",
          url:"edit.php",
        });
        return false;
      }
    </script>
 </body>  
</html>

Following line is incorrect.

echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('\".$username.\"')\"> <a href=\"delete.php\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";

You need to concatenate $username in the string so the quotes around it shouldn't be escaped with \\ backslash.

Here's the modified code.

echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('".$username."')\"> <a href=\"delete.php\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>"; 

使用此命令行尝试代码

<input type=\"button\" value=\"Edit\" onClick=\"editItem('".$username."')\"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM