Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and get php value to jquery

Tags:

jquery

php

I have one problem in passing the value, and I can't pass it through the query string. My PHP is:

 <div class="menu">
        <div class="sub_menu_header">ABOUT</div>
        <?php 
            $query1="SELECT id,title FROM aboutus_tbl";
            $resulto=mysql_query($query1);
            while ($row = mysql_fetch_array($resulto)) {
        ?>
        <a href="about" class="menuid" id="<?php echo $row['id'];?>"><div class="<?php echo $row['title'];?>" id="sub_menu"><?php echo $row['title'];?>
        </div></a>
        <?php
        }
        ?>
    </div>

jQuery:

 <script>
    $(".menuid").click(function() {
        date_time = $(this).attr('id');
        console.log(data);
        $.ajax({
            type: 'POST',
            url: 'about',
            data: {"aboutus_id" : date_time},
            success: function(data){
                alert( data );
            }
        });
    });
    </script>

Full code:

<?php ob_start(); include('web/header.php');?>
<!--main-->
<div class="main_btm">
<div class="wrap">
<div class="main">
    <div class="new_head"><font size="5" color="#FC2B5F">
    <script>
    $(".menuid").click(function() {
        date_time = $(this).attr('id');
        console.log(data);
        $.ajax({
            type: 'POST',
            url: 'about',
            data: {"aboutus_id" : date_time},
            success: function(data){
                alert( data );
            }
        });
    });
    </script>
    <?php
    if (isset($_POST['aboutus_id'])) {
        $id=$_POST['aboutus_id'];
        echo $id;
    }
    else{
        $id=1;
    }
    $que="SELECT title FROM aboutus_tbl WHERE id='".$id."'";
    $re=mysql_query($que);
    $r=mysql_fetch_array($re);
    echo $r[0];
    ?>
    </font></div>
    <div class="menu">
        <div class="sub_menu_header">ABOUT</div>
        <?php 
            $query1="SELECT id,title FROM aboutus_tbl";
            $resulto=mysql_query($query1);
            while ($row = mysql_fetch_array($resulto)) {
        ?>
        <a href="about" class="menuid" id="<?php echo $row['id'];?>"><div class="<?php echo $row['title'];?>" id="sub_menu"><?php echo $row['title'];?>
        </div></a>
        <?php
        }
        ?>
    </div>
    <div class="contant">
    <div class="con_details">
    <?php
        $query="SELECT * FROM aboutus_tbl WHERE id='".$id."'";
        $res=mysql_query($query);
        $result=mysql_fetch_array($res);
        $d1_per=explode("  ", $result['desc1']);
        $d1_lenth=sizeof($d1_per);
        $d2_per=explode("  ", $result['desc2']);
        $d2_lenth=sizeof($d2_per);
        $d3_per=explode("  ", $result['desc3']);
        $d3_lenth=sizeof($d3_per);
        if ($result['sub_title1']) {
    ?>
    <legend><font size="5"><?php echo $result['sub_title1'];?></font></legend>
    <?php
    }
    if ($result['desc1']) {
    for ($i=0; $i < $d1_lenth; $i++) { 
        echo "<p>".$d1_per[$i]."</p>";
    }
    }
    if ($result['sub_title2']) {
    ?>
    <legend><font size="5"><?php echo $result['sub_title2'];?></font></legend>
    <?php
    }
    if ($result['desc2']) {
    for ($i=0; $i < $d2_lenth; $i++) { 
        echo "<p>".$d2_per[$i]."</p>";
    }
    }
    if ($result['sub_title3']) {
    ?>
    <legend><font size="5"><?php echo $result['sub_title3'];?></font></legend>
    <?php
    }
    if ($result['desc3']) {
    for ($i=0; $i < $d3_lenth; $i++) { 
        echo "<p>".$d3_per[$i]."</p>";
    }
    }
    ?>
    </div>
    </div>
</div>
</div>
</div>
<script type="text/javascript">
window.onload = function() {
    $(document).ready(function() {
        $('.<?php echo $result['title']?>').addClass('sub_menu_selected');
    });
}
</script>
<?php include('web/footer.php'); ob_flush();?>

In the above code, I display a menu in a <div> and <a> tags outside so I can click it. My problem is when I click on the menu it must take the id of the menuid class but it's not working and $_POST['aboutus_id'] is not getting any value.

like image 970
Divyesh Jesadiya Avatar asked Aug 17 '15 07:08

Divyesh Jesadiya


People also ask

How set PHP variable value in jquery?

If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.

Can JavaScript access PHP variables?

We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies. Cookie work in client-side.

How to pass variables between JavaScript and PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

How can get jquery variable value in PHP without Ajax?

How can get jquery variable value in PHP without AJAX? Create an hidden input tag in HTML , then set it's value as a JSON string with jquery. Next whenever you need that data , simply call the PHP function which returns value of input tag . Now you can convert the JSON data and store it in PHP variable.


1 Answers

try this code:- if you want to redirect the user:

header('Location: http://ashvin.com/page.php?' . http_build_query($_GET, '', '&')); die();

If however you just want to fetch the page, use this:

file_get_contents('http://ashvin.com/page.php?' . http_build_query($_GET, '', '&'));

and another:-

<a href="/exsomeurl.php?id=1&name=Jose" class="ajax-link"> Click </a>
<a href="/exsomeurl.php?id=2&name=Juan" class="ajax-link"> Click </a>
<a href="/exsomeurl.php?id=3&name=Pedro" class="ajax-link"> Click </a>
...
<a href="/exsomeurl.php?id=n&name=xxx" class="ajax-link"> Click </a>

<script type="text/javascript">
$(function() {
   $('.ajax-link').click( function() {
         $.get( $(this).attr('href'), function(msg) {
              alert( "Data Saved: " + msg );
         });
         return false; // don't follow the link!
   });
});
</script>
like image 58
Ashvin Monpara Avatar answered Nov 15 '22 16:11

Ashvin Monpara