2016-03-02 117 views
1

需要幫忙。我想讓內部PHP腳本在頁面加載時自動運行。我遇到了將表單中Date和User輸入的值傳遞給內部PHP腳本的問題。我已經嘗試了$_POST$_GET。我應該把腳本放在外部腳本中嗎?我真的不需要代碼,只是告訴我該看什麼。謝謝!
輸入登錄/編輯 PHP腳本在頁面加載時自動運行

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
<script type="text/javascript" type="text/javascript" src="js/simpledtpicker.js"></script> 
</head> 

<body style="text-align:center;"> 
<div class="wrapper"> 
<?php include('includes/header.php')?> 
<h1 style="text-align:center;">Entry Log/Edit</h1> 
<span> 
<form action="#" method="GET"> 
<table style="width:100%;"> 
<tr> 
    <th style="text-align:left;"> 
    Entries for: 
    <input type="text" name="dateOfEntry" class="dateOfEntry"> 
    <script type="text/javascript"> 
      $(function() 
      { 
       $('*[name=dateOfEntry]').appendDtpicker(
       { 
        "dateOnly": true, 
        "dateFormat": "MM/DD/YYYY", 
        "autodateOnStart": true, 
        "closeOnSelected": true 
       }); 
      }); 
    </script>   
    </th> 
    <th style="text-align:right; padding-right: 0px;"> 
    User: 
    <input type="text" name="user" value="Hrobi7198228" readonly="readonly" style="border:none; background-color:transparent"/> 
    </th> 
    <th width="5px;"><input type="submit" name="submit" value="Go"></th> 
</tr> 
</table> 
</form> 
</span> 
<br> 
<table class="entry" style="width: 100%;"> 
<thead> 

<?php 
include ('connServer.php'); 

    $date = date('Y-m-d', strtotime(str_replace("-","/",$_GET['dateOfEntry']))); 
    $username = $_GET['user']; 

    $query = 'SELECT `ID`, `Date`, `Description`, `TypeOfDowntime`, `Machine#` FROM `machineissuesreport` WHERE `Date`="'.$date.'" AND `UpdatedBy` = "'.$username.'" ORDER BY `ID` DESC'; 

    $conn = mysqli_query($connection, $query); 

    if ($conn) 
    { 
     echo '<tr> 
       <th width="5px">Edit</th> 
       <th width="5px">Delete</th> 
       <th>Date</th> 
       <th>Details</th> 
       <th width="100px" style="text-align:center;">Downtime Type</th> 
       <th width="20px" style="text-align:center;">Machine No.</th> 
       </tr> 
       </thead> 
       <tbody>'; 
     while($row = mysqli_fetch_array($conn)) 
     { 
      echo '<tr>'; 
      echo '<td style="text-align: center" width="5px"><a href="editMachineIssue.php?id='.$row["ID"].'" class="edit">Edit</a></td>'; 
      echo '<td style="text-align: center" width="5px"><a href="#" id="'.$row['ID'].'" class="delete">Delete</a></td>'; 
      echo '<td style="display: none;"><input type="hidden" value='.$row['ID'].'></td>'; 
      echo '<td width="5px" style="text-align: center; padding: 0px 5px;">'.$row['Date'].'</td>'; 
      echo '<td style="text-align: left; padding: 0px 0px 0px 12px;">'.$row['Description'].'</td>'; 
      echo '<td style="text-align: center;">'.$row['TypeOfDowntime'].'</td>'; 
      echo '<td style="text-align: center;">'.$row['Machine#'].'</td>'; 
      echo '</tr>'; 
     } 
    } 


?> 

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $.ajax 
    ({ 
     type: 'POST', 
     data: {'run':run}, 
     success: function(data) 
     { 
     } 
    }); 

    $('.delete').click(function() 
    { 
     if(confirm("Are you sure you want to delete this row?")) 
     { 
      var del_id = $(this).attr('id'); 
      var $ele = $(this).parent().parent(); 

      $.ajax({ 
       type: 'POST', 
       url: 'machineEntryLogEdit.php', 
       data: {'del_id' : del_id}, 
       success: function(data) 
       { 
        $ele.fadeOut().remove();    
       }, 
       error: function (xhr, status, error) 
       { 
        alert(this); 
       } 
      }); 
     } 
    }); 
}); 
</script> 
</tbody> 
</table> 
</div> 
</body> 
</html> 

我仍然在進步就可以了,但請幫我找到正確的條款,谷歌。我對PHP和AJAX相當陌生。

+0

您是否看到錯誤? – Phil

+0

是,它說 '說明:未定義指數:dateOfEntry在C:\ XAMPP \ htdocs中\ hgstProject \ machineEditEntry.php線路54上 說明:未定義指數:用戶在C:\ XAMPP \ htdocs中\ hgstProject \ machineEditEntry。 55線上的php' –

+0

雖然沒有聲明嗎?我認爲這個問題是在第54行。你打電話strtotime可能沒有設置的東西。你應該把它包裝在if檢查中。 – Phil

回答

0

您可以在js文件中編寫以下代碼,並將js文件包含在您的頁面中。

$(document).ready(function(){ 
$.ajax({ url: "file.php", 
    data: data1, 
    success: function(){ 
     // Do whatever you want to do here. 
    }}); 
}); 
+0

和URL指的是這個特定的PHP? –

+0

是的,url意味着你想要在頁面加載時執行的特定的php文件。 – Peeje