【问题标题】:Pass Variable From HTML Page To PHP Script将变量从 HTML 页面传递到 PHP 脚本
【发布时间】:2012-05-17 12:14:41
【问题描述】:

我想知道是否有人可以帮助我。

下面的代码允许用户从图库中删除图像。

完整的脚本减去样式

 <?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>

本质上,用户单击删除图标,然后上面代码中调用的“delete.php”脚本从服务器中删除图像。这一切都很好。

我正在努力解决的是如何将两个变量值传递给“delete.php”脚本。它们是“userid”和“locationid”。

我尝试将以下内容添加到“delete.php”的开头:

<?php session_start(); 

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

但是这些值没有被结转。我怀疑这可能是因为表单“POST”操作被用于导航到另一个 HTML 页面,尽管我不是专家,所以我可能弄错了。

我已经阅读并搜索了有关如何解决此问题的教程,但我没有找到任何似乎建议解决方案的内容。

我只是想知道是否有人可以看看这个,并就如何将这两个值传递给我的“delete.php”脚本提供一些指导。

非常感谢和亲切的问候

【问题讨论】:

  • 检查我的答案..这是你想要的吗?
  • 嗨@sujal,非常感谢您花时间回复我的帖子和解决方案,我只是在测试。我已经在你的帖子下更新了我的答案。

标签: php variables global-variables


【解决方案1】:

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

{ image : img.attr('src'),  userid: "VALUE", locationid: "VALUE"},

【讨论】:

  • 嗨@Adam,非常感谢您抽出宝贵时间回复我的帖子。您能否告诉我,我的“delete.php”文件中是否有一种方法可以显示这些值被正确提取?亲切的问候
【解决方案2】:

尝试从您的 html 页面发送 id,例如

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

然后在php中

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

如果您想为每张图片动态获取用户 ID 和 locationid。

第一;在你 btn-delete 类中添加 uid 和 locid 属性。我想你正在循环使用 PHP。

喜欢 ===>

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

然后在你的脚本中

    <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');

【讨论】:

  • 嗨@Sandy Frank,感谢您抽出宝贵时间回复我的帖子。不幸的是,“当前位置”中“当前用户”的值和相关性,因此它们会发生变化,我需要考虑到这一点。亲切的问候
  • 您所说的“'当前位置'中的'当前用户'的价值和相关性,因此他们会改变”是什么意思?
  • 嗨@Sandy Fark。我很抱歉没有让这个更清楚。我对此很陌生,所以也许我不完全理解,所以如果我误解了,请纠正我。在您的解决方案中,您建议发送 id,但我是否认为这些值是静态的,必须为每个用户手动更改,或者这些值是否会自动通过?亲切的问候
  • 我刚刚添加了解决方案,如果您发现困难,请告诉我。
  • 嗨@Sandy Fark。非常感谢。请原谅我的提问,但由于我的 'userid' 和 'locationid' 值位于两个隐藏的文本字段中,我可以省略对按钮进行任何更改,但仍然在 url 中显示这些字段:即 url:'delete.php?userid ='+uid+'&locationid='+locid,正如您在更新的解决方案中显示的那样?亲切的问候
【解决方案3】:
 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');
                }
          });

添加 dataType : 'json' 并在 delete.php 中获取发布值

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

【讨论】:

  • 嗨@sujal,非常感谢。不幸的是,该解决方案不起作用。可悲的是,我的 onclick 事件不再删除物理图像。另外,请原谅我的提问,因为我对 PHP 比较陌生,但请你告诉我。有没有一种方法可以让我回显“delete.php”脚本中的值以表明它们被正确拉出?非常感谢和亲切的问候
猜你喜欢
  • 2015-04-11
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2014-12-11
  • 1970-01-01
相关资源
最近更新 更多