2

I have created a table using data from a database, to create it I used a loop to get every piece of data from the columns I need. Now, I have called a javascript function in the HTML onClick of the input. But, I keep getting an error. The function does seem to work, It's just the actual echo that seems to be giving the error. It's on line 58 where you can see I am creating a table and inserting the rows.

UPDATE

My javascript function is working. The error is when it tries to set the <td> width and alignment. The error I get is:

Uncaught SyntaxError: Invalid or unexpected token

<?php
    ob_start();
    session_start();
    require_once 'dbconnect.php';
    if( !isset($_SESSION['user']) ) {
        header("Location: index.php");
        exit;
    }
    $deny = array("222.222.222", "333.333.333");
    if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
       header("location: http://www.google.com/");
       exit();
    }
    $res=mysqli_query($con,"SELECT * FROM users WHERE userId=".$_SESSION['user']);
    $userRow=mysqli_fetch_array($res);
?>
<!DOCTYPE html>
<html>
    <?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
    <head>
        <title>ServiceCoin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
        <link rel="stylesheet" href="scripts/home/index.css" />
    </head>
    <body>
        <ul>
            <li><a href="#" class="a">ServiceCoin.com(image)</a></li>
            <li><a href="logout.php?logout" class="a">Sign Out</a></li>
            <li><a href="#" class="a">Contact</a></li>
            <li><a href="#" class="a">Get Service Coins</a></li>
            <li><a href="#" class="a">News</a></li>
            <li><a href="settings.php" class="a">Settings</a></li>
            <li><a href="#" class="a">Referrals</a></li>
            <li><a href="service.php" class="a">Services</a></li>
            <li><a href="home.php" class="a">Home</a></li>
        </ul>
        <br /><br />
        <center>
        <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userServices']; ?></span> Services</h3>
        <p id="error"></p>
        <button onclick="send_coins()" class="button">Send Coins</button>
        <button onclick="create_service()" class="button">Create A Service</button>
        <button onclick="send_coins()" class="button">My Services</button>
        <h3>View Services</h3>
        <span><?php 
        $users = 1;
        $num = 1;
        echo "<center><table width=1000><th>Buy</th><th>Service Name</th><th>Service Cost</th><th>Service Description</th><th>Service Provider Name</th>";
        while($users < 100 && $num < 100){
            $res=mysqli_query($con,"SELECT * FROM users WHERE userId=".$users);
            $userRow=mysqli_fetch_array($res);
            $id = 0;
            while($id != 10){
                $id = $id + 1;
                if($userRow['userService'.$id] === 'null'){
                }else if(!empty($userRow['userService'.$id])){
                echo "<tr class=services ><td name=".$num."><input type=submit onClick='buy(".$userRow['userService'.$id].",".$userRow['userServiceCost'.$id].",".$userRow['userServiceEmail'].")'></td><td width='250' align='center'>".$userRow['userService'.$id]."</td><td width='250' align='center'>".$userRow['userServiceCost'.$id]."</td><td width='250' align='center'>".$userRow['userServiceDes'.$id]."</td><td width='250' align='center'>".$userRow['userServiceName'.$id]."</td></tr>";  

                //echo $id."Error: ".$con->error;           
                $num = $num + 1;
                }
            }
            $users = $users + 1;
        }
        echo "All Services Loaded";
        echo "</table></center>";
        ?></span>
            <span class="text-danger"><?php echo $msg; ?></span>
        </center>
    </body>
    <script lang="text/javascript" src="scripts/home/index.js"></script>    
    <script type="text/javascript">


function buy(sid, scost, semail){
    console.log(sid,scost,semail);
}

</script>
</html>
<?php ob_end_flush(); ?>
10
  • 1
    Possible duplicate of How to call a JavaScript function from PHP? Commented Nov 17, 2016 at 7:50
  • Look at the edit please Commented Nov 17, 2016 at 7:54
  • <td width='250' align='center'>" place single quotes as shown, see if that helps. Commented Nov 17, 2016 at 7:56
  • No, that doesn't seem to be working. I still get the same error. I will put it in the question Commented Nov 17, 2016 at 7:58
  • can you please post this line echo "<tr class=services ><td name=$num<input type=submit onClick=buy(".$userRow['userService'.$id].",".$userRow['userServiceCost'.$id].",".$userRow['userServiceEmail'].")>Buy</td><td width=250 align=center>".$userRow['userService'.$id]."</td><td width=250 align=center>".$userRow['userServiceCost'.$id]."</td><td class=services width=500 align=center>".$userRow['userServiceDes'.$id]."<td width=500 align=center>".$userRow['userServiceName'.$id]."</tr>"; of code in HTML by inspect element? @morgan Commented Nov 17, 2016 at 8:11

1 Answer 1

1

As I mentioned in the comments above, you need to use quotes for all attributes and JS calls. Additionally, rembember that the contents of your onClick must be valid JS. As such, the strings you are passing as arguments to the function should be in quotes.

echo '<tr class="services"><td name="'.$num.'"><input type="submit" onClick="buy(\''.$userRow['userService'.$id].'\',\''.$userRow['userServiceCost'.$id].'\',\''.$userRow['userServiceEmail'].'\')"></td><td width="250" align="center">'.$userRow['userService'.$id].'</td><td width="250" align="center">'.$userRow['userServiceCost'.$id].'</td><td width="250" align="center">'.$userRow['userServiceDes'.$id].'</td><td width="250" align="center">'.$userRow['userServiceName'.$id].'</td></tr>';

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

1 Comment

Yeah sorry i noticed that and forgot to update the code on here. Sorry, that didn't fix it

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.