Home » Php » How to get the input value and post it in the same php file?

How to get the input value and post it in the same php file?

Posted by: admin June 30, 2018 Leave a comment

Questions:

I want to load the images from the external php file.In my home.php I have two frames left side and right side.In left side I load the images using load function load(image.php) and load the image from image.php file.Then When I click the image,the related image will be displayed in right side.So i write the onclick function in my image.php file.In image.php file I am using ajax function to call the imagedisplay.php file to display the related image in right side in my home.php.Its all working fine. But now I want to get the id and post it into another php file.In my home.php file <input id="id" name="id" value="" type="text"/> in this input tag I got the id value and post the values.

<?php $idnew = $_POST['id'];
echo $idnew; ?>

echo this value I got the error

home.php

  <script>
 $(document).ready(function() {
         $('#image').load('image.php');
    });
</script>      


      <div id="image" class="col-sm-2 col-xs-12">
    <div id="right"  style="width: 100%;height:400px;margin-top: 14%;">

      <p id="rrr" style="margin-top: 26%;">Image.</p>
      <img src="" id="rightimage" name="image"/>

                            <input id="id" name="id" value="" type="text"/>
<?php  $idnew = $_POST['id'];  
echo $idnew;  ?>
                            </div>

image.php

      <?php
             $sideimagethumbnail = "SELECT * FROM others WHERE  username = '$username' AND status = '1'";

              $result= $conn->query($sideimagethumbnail);

                        if($result -> num_rows > 0)
                                    {

           while($row = $result->fetch_assoc())
                                        {

                                        $id = $row['id'];
                                 $imagename = $row['imagename'];

                                          ?> <a onclick="image('<?php echo $id ; ?>')"><img src="<?php echo $imagename;?>" class="img-responsive" alt="img" /></a>
    <?php } } ?>

 <script>
    function image(id)
    {
        var hh = id;

         $.ajax({
                 type : "POST",
                 url:"imagedisplay.php",
                 data:"id=" + hh ,
                 success: function(result)
                 {
                    $('input[name=id]').val(id);

                    document.getElementById("rrr").style.display="none";
                    $('#rightimage').attr("src",""+result); 
                 }

            });
    }


    </script>

imagedisplay.php

$targetid = $_POST['id'];
    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
                       if ($conn->connect_error) 
                       {
                           die("Connection failed: " . $conn->connect_error);


                       }
                       $conn->autocommit(FALSE);   

                                $query2 = " SELECT * FROM " . TB_OTHERS . " WHERE " .KEY_ID. "= $id";



                              $result21 = $conn->query($query2);

                              if($result21->num_rows >0)
                            {

    while($rowquerycat2 = $result21->fetch_assoc())
                                {
                                    $image =  $rowquerycat2[KEY_IMAGENAME];
                                    echo $image;
                                    }
                                    }
Answers: