0

したがって、ページに3つのフォームがあり、送信されたフォームに応じて、DBに何かが入力されます。さて、このコードをすべて同じページに含めると完全に機能しますが、もちろん更新が必要なので、AJAX の実装を検討しています。今、挿入 php コードを新しいページに移動しました。データを php ページに渡すための AJAX 呼び出しを作成するためのヘルプを探しています。これにより、正しいデータが挿入されます。

基本的に:

  1. 各アクティビティ タイプに 1 つずつ、合計 3 つのフォームがあります。
  2. 送信されたフォームに応じて、いくつかの異なるデータが DB に入力されます
  3. どのフォームが送信されたかを把握するための AJAX 呼び出しの作成に助けが必要です。PHP コードが DB に挿入できるように、データを次のページに正常に渡します。
  4. PDO を使用して SQL を書き直しますので、現在のコードでは無視してください。

    <form id="form1"  method="post">
    <input type="submit" id="activity1" name="activity1" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type1);?>">
    </form>
    
    <form id="form2" action="registerresults.php" method="post">
      <input type="submit" id="activity2" name="activity2" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type2);?>">
    </form>
    
    <form id="form3" action="registerresults.php" method="post">
      <input type="submit" id="activity3" name="activity3" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type3);?>">
    </form>
    

    //registerresults.php - DB への挿入

         $id = $_SESSION['id'];
         $competitionId = $_GET['competitionId'];
         $organisationId = $_SESSION['organisationId'];
    
    
        if (isset($_POST['activity1']) && !empty($_POST['activity1']))
        {
            //insert new points into database
            $today = date("Y-m-d h:i:s");
            $insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type1', '1', '$weighting1', '$today');");
    
        }
    
        if (isset($_POST['activity2']) && !empty($_POST['activity2']))
        {
            $today = date("Y-m-d h:i:s");
            $insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type2', '2', '$weighting2', '$today');");
    
        }
    
        if (isset($_POST['activity3']) && !empty($_POST['activity3']))
        {
            $today = date("Y-m-d h:i:s");
            $insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type3', '3', '$weighting3', '$today');");
    
        }
    ?>
    
4

0 に答える 0