-1

From the below code I need to display the values of data1. I have declared it by using id as "id="data1". Suggest me how to pass this "data1" as a variable in phpMysql.

            <div class="col-lg-12">
            <p id="data1"></p>

<?php

// Make a MySQL Connection

mysql_connect("localhost", "projects", "pwd", "projects") or die(mysql_error());
                mysql_select_db("projects") or die(mysql_error());

                $var='data1';

                // Get all the data from the "Race" table and create table

                $result2 = mysql_query("SELECT 
                                            A.service_center_name,
                                            A.status,
                                            C.branch_name
                                        FROM
                                            customers A
                                                INNER JOIN
                                            ascs B ON A.serv_cent_mob_no = B.contact_number
                                          Inner Join
                                             branches C on B.branch_id=C.id  
                                             where C.branch_name='". $var. "'
                                        GROUP BY A.service_center_name ,A.status,C.branch_name;")
                or die(mysql_error());

                echo "<table border='1'>";
                echo "<tr> <th>Service Center Name</th> <th>City</th> <th>Branches</th>  </tr>";

                // keeps getting the next row until there are no more to get

                while($row = mysql_fetch_array( $result2 )) {
                // Print out the contents of each row into a table
                echo "<tr><td>";
                echo $row['service_center_name'];
                echo "</td><td>";
                echo $row['branch_name'];
                echo "</td><td>";
                echo $row['status'];
                echo "</td></tr>";
                }
                echo "</table>";
      ?>
        </div>

How to pass the data1 using variable in the below code "$var='data1';".

sharmila
  • 175
  • 2
  • 19
  • Don't use `mysql` extension, refer [this](http://php.net/manual/en/function.mysql-connect.php) – Mahesh.D Jun 12 '18 at 10:06
  • You seem to be confusing PHPMySQL (a MySQL database client that happens to be written in PHP) with the PHP `mysql_` API (an obsolete API for accessing a MySQL database from your own code). – Quentin Jun 12 '18 at 10:10
  • What is the update on this? I have been really busy, so I haven't had the time to follow up on this. Did you ever solve this issue? – Martin Jul 10 '18 at 07:27

2 Answers2

0

Solution:

You can use the jQuery AJAX function to parse the data to the desired file of your choosing. More about jQuery AJAX here.

Your AJAX function could look like so:

function postData() {
    var data = $('#data1').html();

    $.ajax({
        type : "POST",
        url: "/some/path/some_page.php",
        data: { dataVariableName : data },
        success: function (html) {
            //Success handling
        }
    })
}

You could then fire the function from a button. For instance:

<button onclick="postData();">Submit data!</button>

In your some_page.php, you will then need to access your POST variable, like so:

<?php
$var=$_POST['dataVariableName'];

//Continue with SQL logic etc.
?>

Explanation:

What we basically did here, is that we encapsulated the AJAX function into another function named, postData, which we can use to call onclick, or however we desire. We could also simply add an onclick event to the ajax function directly, but I thought this would make for an easy understanding.

We then go on to define a variable that contains the data we wish to parse.

Then in our AJAX function, we first define our data type. As you can see in this example, we're using the data type POST. There are other data types that you can define here, and each for a different purpose. Another well-known data type would be GET for instance. I suggest you look up the data types to find out what they mean, and what influence they have. For instance, GET types will show as parameters in the URL.

Next we define what page we are sending our data to, which will be some_page.php in our example.

We then go on to define our POST variable, which is going to contain the data we're supposed to parse. You can parse more than one variable at a time in your AJAX function, by doing so:

data: {
  dataVariableName : data,
  dataVariableName2 : otherData,
  //more variables [...]
},

Note that I also defined a success function in our AJAX function. We can use this to do a lot of things upon success, if we so desire. I.e. redirect to another page, alert(); a success message etc. etc. A lot of things.

If you run into trouble with the SQL, let me know, and I can take a look at that as well.


Important note:

You should really consider switching to mysqli_* or PDO, instead of using the deprecated mysql_* notation. You won't be able to use the mysql_* notation in the newer version of PHP, i.e. PHP 7.0 and forward. You should also look into prepared statements and sanitizing your inputs in general, in case you continue with the mysql_* notation.

Martin
  • 2,326
  • 1
  • 12
  • 22
-1

using Jquery you can get the data in p tag like below

var pdata = $('#data1').html();

you can post this data to php using jquery Ajax as below

request = $.ajax({
    url: "/form.php",
    type: "post",
    data: pdata
});

In your php, you can make it as $var = $_POST['data'];

sree
  • 389
  • 5
  • 17
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 12 '18 at 10:11
  • This won't work anyway. The PHP reading the database runs **immediately** after the PHP that generates the HTML. It isn't two separate HTTP requests. – Quentin Jun 12 '18 at 10:14