繁体   English   中英

在同一页面上将javascript变量从模式传递给php

[英]Passing javascript variable from modal to php on the same page

我实际上每天都在解决这个问题,我只想对此脚本执行编辑操作。我的主页称为customer-grid-screen.php,代码如下

<?php include("header.inc.php");
include("left.inc.php");
include('config.php');?>

<div id="page-content-wrapper">
  <?php include("pagetitile.inc.php"); ?>
  <div id="page-content">
  <div class="clearfix mrg10B"><a href="javascript:;"   class="btn large bg-green float-right modal-customeradd" title=""><span class="button-content">Add</span></a></div>
<div class="example-box">
    <div class="example-code">

        <table class="table table-condensed">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Details</th>
                    <th>Domain</th>
                    <th>Vertical</th>
                    <th>Taxanomy</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>

<?php
$result = mysql_query("SELECT * FROM customer_mast ORDER BY customer_id") or trigger_error(mysql_error());

while($row = mysql_fetch_array($result)){ 
if($row<1) {
    echo "</tr>";
    echo "<tr>";
    echo "<td colspan='6' class='text-center pad25A'>No Record</td>";
    echo "</tr>";
}
else
{
foreach($row AS $key => $value){
    $row[$key] = stripslashes($value);
  } 
    echo "<tr>";  
    echo "<td><a href='customer-screen.php'>" . nl2br( $row['customer_name']) . "</td>";

    if(!empty($row['customer_details'])){
    echo "<td><a href='customer-screen.php'>". nl2br( $row['customer_details']) . "</td>";
        }
    else{
       echo "<td><a href='customer-screen.php'>-</td>";
    }
    if(!empty( $row['domain']))
    {
    echo "<td><a href='customer-screen.php'>". nl2br( $row['domain']) . "</td>";}
    else{
       echo "<td><a href='customer-screen.php'>-</td>";
    }
    if(!empty($row['vertical'])){
    echo "<td><a href='customer-screen.php'>". nl2br( $row['vertical']) . "</td>";}
    else{
        echo "<td><a href='customer-screen.php'>-</td>";
    }
    if(!empty($row['taxanomy'])){
       echo "<td><a href='customer-screen.php'>". nl2br( $row['taxanomy']) . "</td>";
    }
    else
    { echo "<td><a href='customer-screen.php'>-</td>";}

         echo $row['customer_id'];


    echo "<td>

    <a href='javascript:?id={$row['customer_id']}'    data-id={$row['customer_id']} class='btn small bg-blue-alt tooltip-button modal-customeredit' data-placement='top' title='Edit'><i class='glyph-icon icon-edit' ></i>

    </a>
    <a href='customer_delete.php?id={$row['customer_id']}'  class='btn small bg-red tooltip-button confirm' data-placement='top' title='Remove'><i class='glyph-icon icon-remove'></i>
   </a>
   </td>";}}


 ?>
            </tbody>
        </table>
      </div>

</div>   
</div><!-- #page-content -->
    </div>
            </div>
<?php include("footer.inc.php"); ?>

我必须通过模式弹出来执行编辑操作。我实现了如下代码。

footer.inc.php
------------


    <!-- Project Edit  MAINLY SEE THIS-->
    <div class="hide" id="modal-projedit" title="Edit Project Info">
    <div class="pad10A">
    <h3>Edit Project Info</h3>
    <p class="font-gray-dark"> Fides Admin uses colors & styles from both the default theme color schemes and the included core color helpers. </p>
    <div class="divider mrg25B"></div>
    <form id="project-edit" action="" class="col-md-12 center-margin" method="">
    <div class="form-row">
    <div class="form-label col-md-3">
    <label for="name">
      Project Name:
      <span class="required">*</span>
      </label>
    </div>
    <div class="form-input col-md-9">
    <input id="name" name="name" placeholder="Name" data-required="true" class="parsley-validated" type="text">
    </div>
    </div>
    <div class="divider"></div>
    <div class="form-row">
    <div class="form-input col-md-8 col-md-offset-3">
    <a href="javascript:;" class="btn medium primary-bg radius-all-4" id="project-edit-valid" onclick="javascript:$('#project-edit').parsley( 'validate' );" title="Validate!">
      <span class="button-content">
        Update
        </span>
      </a>
    </div>
    </div>
    </form>
    </div>
    </div>
//modal window script:
 $( ".modal-customeredit" ).click(function() {


       var myGroupId = $(this).data('id');
          alert( myGroupId); //i can able to alert the paricular row id i want to edit i dont know to pass it through php.


   $( "#modal-customeredit" ).dialog({
     modal: true,
     minWidth: 700,
     minHeight: 200,
     dialogClass: "modal-dialog",
     show: "fadeIn"
   });
   $('.ui-widget-overlay').addClass('bg-black opacity-60');
 });        


//same page php file

<?php
   $id=???; (get id not working)

     $sql="SELECT * FROM `customer_mast` where customer_id='$id'";
   $result=mysql_query($sql);
   if($result==false)
      {
      die(mysql_error());
      }
      else{
      $row  = mysql_fetch_array($result);}
       if (isset($_POST['submit'])){
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
$sql = "UPDATE `customer_mast` SET   `customer_name` =  '{$_POST['customer_name']}' ,  `customer_details` =  '{$_POST['customer_details']}',`domain` =  '{$_POST['domain']}' ,`vertical` =  '{$_POST['vertical']}' ,`taxanomy` =  '{$_POST['taxanomy']}'   WHERE `customer_id` = '$id' "; 
mysql_query($sql) or die(mysql_error()); 
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />"; 

header("Location:customer-grid.php");}
  ?>

任何人都可以请我解释一下如何将该id值传递给同一页面上的php脚本并执行我的编辑操作。如果您有疑问,我可以进行更多解释,我已经经历了很多事情来使其正确。没有任何帮助。请提出您的建议。

您应该了解AJAX如何将值传递给PHP。

$( "#modal-customeredit" ).dialog({
     modal: true,
     minWidth: 700,
     minHeight: 200,
     dialogClass: "modal-dialog",
     show: "fadeIn"
     buttons: {
         Edit: function() {
           // Sample ajax request ( see the documentation)
           $.ajax({
               type: "POST",
               url: "request.php",
               data: { id: myGroupId }, // data retrieve on server-side ($_POST['id'])
               dataType : 'html|json|...' // expected data returned
           })
           .success(function( msg ) {
               alert( "Data Saved: " + msg );
           });
         },
         Cancel: function() {
           $( this ).dialog( "close" );
         }
    }
    // stop event when form is submit
    return false;
});

您拥有自己做的所有元素。 仅提供另一条建议,请尝试分离文件。 例如,在另一个文件中以request.php执行您的处理。

编辑:

您的request.php的示例(我将返回json)。

<?php
   header('Content-type: application/json'); // Tell the app that you want return json
   $id= $_POST['id']; // correspond to your retrieved data in ajax function

   // ... Your SQL request and other treatment

   // The result return
   echo json_encode($result);
?>

现在,数据将以.success(data)发送回您的ajax函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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