2014-03-06 80 views
0

實際上我正在用這個問題解決一天。我只是想對這個腳本執行編輯操作。我的主頁叫做customer-grid-screen.php 代碼來如下在同一頁上將JavaScript變量從模態傳遞到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"); ?> 

我不得不通過模式彈出up.i進行編輯操作實現的代碼如下。

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腳本和我的[執行編輯operation.I可以解釋更多,如果你有疑問,我經歷了許多事情作出了它是正確的。沒有幫助我。請給你的建議。

回答

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

數據現在會發送回您的ajax函數.success(data)。

+0

你可以更清楚我必須在我的情況下,在它的footer.inc.php在同一頁上執行請求。 – user3386898

+0

我會把所有的PHP代碼(目標是執行一個sql語句)放在另一個名爲** request.php **的頁面中。 AJAX允許您通過傳遞變量myGroupId中的id(s?)來調用頁面(無需重新加載頁面)。 – Debflav

+0

你可以給我一個小小的演示,或者你可以通過把它放在另一個頁面上來測試我的代碼。這對我來說真的很有幫助。 – user3386898