简体   繁体   中英

Pass Variable From HTML Page To PHP Script

I wonder whether someone may be able to help me please.

The code below allows users to delete images from a gallery.

Full Script Minus Styling

 <?php session_start(); 

$_SESSION['userid']=$_POST['userid'];
$_SESSION['locationid']=$_POST['locationid'];

//echo $_SESSION['userid']; 
//echo $_SESSION['locationid'];
?>
<?php 

  $galleryPath = 'UploadedFiles/' . $_SESSION['userid'] . '/' . $_SESSION['locationid'] . '/';

  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

  $descriptions = new DOMDocument('1.0'); 
  $descriptions->load($absGalleryPath . 'files.xml'); 
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=0.3">
        <title>Galleria Twelve Theme</title>
        <style>


        <script src="js/jquery-1.7.2.min.js"></script>
        <script src="js/jquery-ui-1.8.19.custom.min.js"></script>
        <script src="galleria/galleria-1.2.7.min.js"></script>
        <script src="galleria/themes/twelve/galleria.twelve.min.js"></script>
        <script src="galleria/plugins/history/galleria.history.min.js"></script>
        <link rel="stylesheet" href="galleria/themes/twelve/galleria.twelve.css">
         <style>

         .galleria-thumbnails .btn-delete {    
         display: block; /* Or just use a div instead of a span*/     
          position: absolute; bottom : 0px;  /*align at the bottom*/     
          width: 80px;     
          height: 17px;    
          cursor: pointer;     
          background: url(trash8.gif) no-repeat bottom; } 
         </style>

<script type="text/javascript"> 
Galleria.ready(function() { 
this.$('thumblink').click(); 

$(".galleria-image").append( 
"<span class='btn-delete ui-icon ui-icon-trash'></span>"); 

$(".btn-delete").live("click", function(){ 

// you do not need to find img and then src... just use id of image 
var img = $(this).closest(".galleria-image").find("img"); 

var userid=$(".hiddenvals").attr('userid'); 
var locationid=$(".hiddenvals").attr('locationid'); 

// send the AJAX request 
$.ajax({ 
url : 'delete3.php?userid='+userid+'&locationid='+locationid, 
type : 'post', 
data : { image : img.attr('src') }, 
success : function(){ 
alert('Deleting image... '); 
img.parent().fadeOut('slow'); 
} 
}); 

return false; 
}); 

}); 

</script> 



    </head>

<body>
<ul id="navigation">
<li class="left">
  <div align="center"><a href="javascript:document.viewimages.submit()" class="style2">&larr; Add Images</a></div>
</li>
</ul>
<form id="viewimages" name="viewimages" class="page" action="index.php" method="post"> <input name="userid" class="hiddenvals" type="hidden" value="<?php echo $_SESSION['userid']; ?>"> <input name="locationid" class="hiddenvals" type="hidden" value="<?php echo $_SESSION['locationid']; ?>"></form>
    <div class="content">
        <h1>Galleria Twelve Theme</h1>
        <p>Demonstrating a simple example.</p>

        <!-- Adding gallery images. We use resized thumbnails here for better performance, but it’s not necessary -->

        <div id="galleria">
          <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) :  
                          $xmlFile = $descriptions->documentElement->childNodes->item($i);  
                          $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8');  
                          $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8');  
                          $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source'));   
                  ?>
            <a href="<?php echo $source; ?>"><img data-title="<b>Image Name: </b> <?php echo $name; ?>" data-description="<b>Description:</b> <?php echo $description; ?>" src="<?php echo $source; ?>"></a>

      <?php endfor; ?>  
</body>
</html>

In essence, the user clicks on a delete icon, then the 'delete.php' script called in the code above deletes the image from the server. This all works well.

What I'm struggling with is how to pass two variable values to the 'delete.php' script. These are 'userid' and 'locationid'.

I've tried adding the following to the beginning of my 'delete.php':

<?php session_start(); 

$_SESSION['userid']=$_POST['userid'];
$_SESSION['locationid']=$_POST['locationid'];

But the values are not carried over. I suspect that this may be down to the fact the the forms 'POST' action is being used to navigate to another HTML page, although I'm no expert, so I may have got this wrong.

I've done quite a bit of reading and searched for tutorials on how to go about getting around this problem, but I've not found anything that seems to suggest a solution.

I just wondered whether someone may be able to look at this please, and offers some guidance on how I can pass these two values to my 'delete.php' script.

Many thanks and kind regards

如果它们在html页面中,则将它们添加到ajax请求中:

{ image : img.attr('src'),  userid: "VALUE", locationid: "VALUE"},
 var userid=$('#userid').val();
 var locationid=$('#locationid').val();
 $.ajax({
                url : 'delete.php',
                type : 'post',
                dataType :  'json',
                data : { image : img.attr('src'),  userid: userid, locationid: locationid},
    ,
                success : function(){
                alert('Deleting image... ');
                img.parent().fadeOut('slow');
                }
          });

add dataType : 'json' and get posted value in delete.php by

$userid=$_POST['userid'];
$locationid=$_POST['locationid'];

Try to send ids from your html page like

 $.ajax({
    url : 'delete.php?userid=2&locationid=4',
   ........

Then in php

$uid=$_POST['userid'];
$locid=$_POST['locationid'];

If you want to get userid and locationid dynamically for each image.

FIRST; in you btn-delete class add uid and locid attribute. I suppose you are looping through PHP.

LIKE ===>

<input type="button" class="btn-delete" uid="2" locid="5">

Then in your script

    <script type="text/javascript"> 
    Galleria.ready(function() {
    this.$('thumblink').click();

    $(".galleria-image").append( 
    "<span class='btn-delete ui-icon ui-icon-trash'></span>"); 

    $(".btn-delete").live("click", function(){

    // you do not need to find img and then src... just use id of image
    var img = $(this).closest(".galleria-image").find("img");

    var uid=$(this).attr('uid');
    var locid=$(this).attr('locid');

    // send the AJAX request
    $.ajax({
    url : 'delete.php?userid='+uid+'&locationid='+locid,
    type : 'post',
    data : { image : img.attr('src') },
    success : function(){
    alert('Deleting image... ');
    img.parent().fadeOut('slow');
    }
    });

    return false;
    });

    });

</script>

var id=$(this).attr('id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM