2015-05-03 38 views
0

我有一个php文件(todo.php & todo.js),它收集用户的一些数据并使用ajax发布收集数据到另一个PHP文件(add.php)被添加到数据库。我的问题是,我不想使用ajax将数据发布到第二个php文件(add.php)。我该如何改变这一点。我已提取的代码和下面如何将数据从一个php文件发布到另一个php文件,并将数据添加到数据库而不使用ajax

//todo.js所示从todo.php称为验证新todoEntry

function addToDo(){ 
    var descField = document.getElementById('nDesc'); 
    var percField = document.getElementById('nPerc'); 

    if(!validateField(descField)){ 
     alert("Description " + errorMessage); 
     return; 
    } 
    if(!validatePercentField(percField)){ 
     alert("Percentage " + errorMessage); 
     return; 
    } 

    //use ajax to post new entries to add.php which will add them 
    ajaxCall(encodeURI('add.php?description=' + descField.value 
      + '&percentage=' + percField.value), function(response){ 
     if(response.status == 200){ 
      if(response.responseText.toString().indexOf 
     ("success") != -1){ 
       //clear values 
       descField.value = ""; 
       percField.value = ""; 

       //refresh table 
       fetchToDo(); 
      } 
      else 
       alert(response.responseText); 
     } 
    }); 
    } 

    From add.php: 
    <?php 
    session_start(); 

    if(!isset($_SESSION['username'])){ 
     die("Your Session has expired. Please re-login to continue"); 
    } 

    //get required data 
    $description = trim(isset($_GET['description']) ? urldecode($_GET 
     ['description']) : ""); 
    $percentage = trim(isset($_GET['percentage']) ? urldecode($_GET 
     ['percentage']) : ""); 

    //validate data 
    if(empty($description) || empty($percentage)){ 
     die("All fields are required!"); 
    } 

    //connect to database 
    include('connect.php'); 

    //insert data 
    $stmt = $mysqli->prepare("INSERT INTO todo_entry VALUES (NULL, ?, NOW 
     (), NOW(), ?, ?)"); 
    $stmt->bind_param("ssi", $_SESSION['username'], $description, 
     $percentage); 
    $stmt->execute(); 

    if($stmt->affected_rows > 0){ 
     $stmt->close(); 
     echo "success"; 
    } 
    else{ 
     echo "An Error Occured: " . $stmt->error; 
     $stmt->close(); 
    } 

    //close database 
    $mysqli->close(); 
    ?> 
+2

创建一个窗体,为第二页和method = post创建操作。您将在$ _POST的第二页获取所有数据。 –

+0

这个逻辑有一个严重的缺点。事实上,你正在谈论你100%知道的非常基本的话题。你在说什么是表单功能。正如@anantkumarsingh所说的那样。生活比你想要的工作流程更容易。 – SaidbakR

+0

是的,如果您不想使用表单加载另一个页面,那么Ajax是唯一的选择。 – Capsule

回答

0

卷曲是PHP作为AJAX是JS。如果您想直接从PHP调用另一个脚本而不需要使用broswer向服务器发出额外请求,则可以使用cURL。这是一个简单的功能。

/* 
* Makes an HTTP request via GET or POST, and can download a file 
* @returns - Returns the response of the request 
* @param $url - The URL to request, including any GET parameters 
* @param $params - An array of POST values to send 
* @param $filename - If provided, the response will be saved to the 
* specified filename 
*/ 
function request($url, $params = array(), $filename = "") { 
    $ch = curl_init(); 
    $curlOpts = array(
     CURLOPT_URL => $url, 
     // Set Useragent 
     CURLOPT_USERAGENT => 
      'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) 
        Gecko/20100101 Firefox/29.0', 
     // Don't validate SSL 
     // This is to prevent possible errors with self-signed certs 
     CURLOPT_SSL_VERIFYPEER => false, 
     CURLOPT_SSL_VERIFYHOST => false, 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_FOLLOWLOCATION => true 
    ); 
    if(!empty($filename)){ 
     // If $filename exists, save content to file 
     $file2 = fopen($filename,'w+') 
      or die("Error[".__FILE__.":".__LINE__."] 
        Could not open file: $filename"); 
     $curlOpts[CURLOPT_FILE] = $file2; 
    } 
    if (!empty($params)) { 
     // If POST values are given, send that shit too 
     $curlOpts[CURLOPT_POST] = true; 
     $curlOpts[CURLOPT_POSTFIELDS] = $params; 
    } 
    curl_setopt_array($ch, $curlOpts); 
    $answer = curl_exec($ch); 
    // If there was an error, show it 
    if (curl_error($ch)) die(curl_error($ch)); 
    if(!empty($filename)) fclose($file2); 
    curl_close($ch); 
    return $answer; 
} 
+0

如果2个PHP文件位于同一个服务器上(OP没有说明,但看​​起来像是因为'add.php'当前ajax URL之前没有任何内容),第二个简单的'include()'会完成这项工作时不用强调http服务器;-) – Capsule

+0

他目前的解决方案是基于AJAX的,这意味着脚本需要某些post/get值。使用include将要求他重写脚本,因为这些值不会出现。但我同意,如果可能的话,包括这将是一个更好的解决方案。 –

+0

更改add.php中的变量名称仍然会比实施一个矫枉过正的卷曲方法更快;-)顺便说一句,你仍然可以在脚本中声明任何包含add.php的$ _POST或$ _GET值,如果你真的不想改变它,并保持add.php可以包含或张贴到的某种双重解决方案。 – Capsule

相关问题