将javascript变量从modal传递到同一页面上的php

时间:2014-03-06 08:25:56

标签: javascript php jquery

我实际上已经解决了这个问题一天。我只想对这个脚本执行编辑操作。我的主页名为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脚本并执行我的编辑操作。如果你有疑问,我可以解释更多,我已经经历了很多事情来使它正确。没有帮助了我。请给出你的建议。

1 个答案:

答案 0 :(得分:0)

您应该看到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(数据)中的ajax函数。