0

I'm trying to figure out how to get a SQL DB write into my file that is largely javaScript and JQuery. I've found some php info online, but I'm having trouble getting the php into my code with everything else that's there. I'm trying to utilize an array where I put user entered info from an html table, and call a method that uses the array as a parameter. I have made my entire file a php file, but I'm having trouble figuring out where to put the php <?php ?> delimeters without having my bigTableRows array go out of scope, or other run-time error messages. After I figure this out I need to do a MS SQL write. Right now, I see this error message, but no table. Line 83 is the line after the <?php, where I have echo(bigTableRows[0];

Parse error: syntax error, unexpected '[' in E:\visE\jqproject\web\BigTable.php on line 83 

If I take away the [0] part, I get a syntaxError, but see the table ok:

missing; before statement.

This is what it looks like:

BigTable.php:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>

<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<meta content="utf-8" http-equiv="encoding"/> 
    <title>Big Table</title>


    <script type="text/javascript" src="js/jquery-1.11.0.js"></script>

    <script type="text/javascript">
        //Used to make row editable or not
        $(document).ready(function () {
      $('.editbtn').click(function () {
          var currentTD = $(this).parents('tr').find('td');
          if ($(this).html() === 'Edit') {                  
              $.each(currentTD, function () {
                  $(this).prop('contenteditable', true);
              });
          } else {
             $.each(currentTD, function () {
                  $(this).prop('contenteditable', false);
              });
          }
          //change button text when hit it between edit/save
          $(this).html($(this).html() === 'Edit' ? 'Save' : 'Edit');

          var bigTableRows = getBigTableRowData(0); //first row
          console.log("bigTableRows other", bigTableRows);
          InsertData(bigTableRows);
      });


    function InsertData($theDataArr)
    {

        //The php part below isn't working**********************            
        $bandY = $theDataArr[0];//$_POST['bandY'];
        $bandM = $theDataArr[1];//$_POST['bandM'];
        $bandC = $theDataArr[2];//$_POST['bandC'];
        $bandK = $theDataArr[3];//$_POST['bandK'];
        $comment = $theDataArr[4];//$_REQUEST['comment'];
        console.log("bandY php: ", $bandY);
        console.log("bandM php: ", $bandM);
        console.log("bandC php: ", $bandC);
        console.log("bandK php: ", $bandK);
        console.log("comment php: ", $comment);
        <?php //where to put this and still have variable info******
        echo(bigTableRows[0]);
            //console.log("bandY php: ", $bandY);
            //console.log("bandM php: ", $bandM);
            //console.log("bandC php: ", $bandC);
            //SQL dB write to follow after I can access data**************
        ?> 
    }
  });
    </script>

    <script>
     function getBigTableRowData(rowNum)
     {
        //I just need row data for the one that was just edited/saved******
        var rowText = $("#bigTable tbody tr:eq(" + rowNum + ") td").map(function() {
        // Find all of the table cells on this row.
            // Determine the cell's row text 
            return $(this).text();
        }).get();    
        return rowText;
     }
    </script>       
</head>
<body>
    <div class="form">
        <p>
            <h1> Visual Evaluation Entry Table </h1>
        </p>
    </div>
<table id="bigTable" border="1">
    <thead>
        <tr>
            <th id="edit" class="col3">Edit/Save</th><th id="bandY" class="col3">Bands @263mm Y</th><th id="bandM" class="col3">Bands @263mm M</th><th id="bandC" class="col3">Bands @263mm C</th><th id="bandK" class="col3">Bands @263mm K</th><th id="Comments" class="col3">Comments</th></tr>
    </thead>
    <tbody>
        <tr>
            <td><button class="editbtn">Edit</button></td>  
            <td name="bandY" contenteditable="false"></td> <!-- //Row 0 Column 1-->
            <td name="bandM" contenteditable="false"></td>  <!--//Row 0 Column 2-->
            <td name="bandC" contenteditable="false"></td>  <!--//Row 0 Column 3-->
            <td name="bandK" contenteditable="false"></td>  <!--//Row 0 Column 4-->
            <td name="comment" contenteditable="false"></td>  <!--//Row 0 Column 4-->
        </tr>
    </tbody>

</table>
</body>

</html>

Some examples I've found are: php sql write, and put html table data into array

I know that if I take away the <?php delimeters, it would run ok (minus echo) (and access the array data), but I need them to do the php part. If I'm off-base on any of this, which I'm sure something is, feel free to let me know. I have a little php experience with xml/html, but I'm learning javaScript and JQuery, and I've never tried to put it all together before.

3
  • No, it's Microsoft SQL Commented Jun 10, 2014 at 11:57
  • The "InsertData" function is suposed to be Javascript or PHP? Commented Jun 11, 2014 at 5:23
  • Javascript. I got the idea from the link above in the question. Commented Jun 11, 2014 at 12:44

1 Answer 1

0

OK, im really trying to be constructive here, i never used the Microsoft SQL connector on PHP but looking on the documentacion is very similar to the MySQL connector (i think is meant to be that way).

So, you need to create the connection first, if your doing it, you need to iterate the dataset to obtain data or pass the data to an array.

In your example code i see a mix of JS with PHP, that's just not right.

the only thing you need to do is read this link: this link!

1.- Create the connection. 2.- run the query. 3.- fetch the dataset to an array (this is only my personal recomendation). 4.- Use the data inside the PHP or save it on a variable like this.

<?php
    echo "var val = " . $myArray[0][0] . ";"; /** Here you are "printing" the variable declaration into the HTML document. **/
?>

In fact, if you want your data on JSON to be used in a javascript object, you can do it this way.

<?php
    echo "var myObj = '" . json_encode($myArray) . "';";
?>

If i missed something let me know.

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

4 Comments

I am going in the other direction. I got the data from the table and need to write it to the SQL DB using PHP.
OK, here's a mayor confusion about PHP and Javascript. I will add another answer to this.
Sorry, a long time has passed but, this is the thing, you are trying to run php code AFTER the page is loaded, and thats imposible, php is server side so you need to save the data in a server instance, like sending the new data through ajax to the php server and update it. the php server first runs all the code and then renders the page, so, after the page is visible on the explorer you cant run php code. You need to create another page to recieve the new info.
Thanks! I had wound up going to another page when I hit the submit button after entering data in the table. Then in that php file I used Post/Get to handle the data and put it in an array of oo tableData objects, then I send it to another php file where I loop through the data to send it to the SQL db.

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.