Multiple Submit Buttons

Hi there,

To be honest, I have lost the overview of where we were with this a little.

The best thing would be for you to implement all of what we have so far (i.e. the new buttons and the remove SMS) and get that working first.
Once you have done that, let me know and we’ll look at the rest.

i have put the dropdowns back in to the script i think i might put the mysql back in to the index.php and see if that returns the data easier

Okay, but it appears you still have some errors.
When I go to the new site and try to submit a new SMS I get:

Parse error: syntax error, unexpected T_CASE in /path/to/submit.php on line 86

When I look at the remove SMS page I see an empty dropdown :frowning:

The console is showing:

Uncaught Error: cannot call methods on selectmenu prior to initialization; attempted to call method 'refresh' 

Im going to try work out whats gone weong with the t case issue and the dropdowns shall I just put the mysql query in index or leave it in submit

It won’t work if you put it in index.php as you will run straight into the problem of needing to refresh the dropdowns.
You need to put in in submit.php, so that it can be refreshed via AJAX.

Like I said, just implement what we have so far and let me know.
We’re 85% of the way there :slight_smile:

This is what i have so far


<?php 
try {
    $conn = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

$sender = $_POST['sender'];

function getLastFiveSMS($conn){
  $stmt = $conn->prepare('SELECT * FROM texts WHERE content IS NOT NULL AND messagesent = 0 LIMIT 5');
  $stmt->execute();
  $options = "";
  
  while($row = $stmt->fetch()) {
    $options .= "<option value='" . $row["id"] . "'>" . $row["content"] . "</option>";
  }
  
  return $options;
}
$sender = $_POST['sender'];
function getadverts($conn){
  $stmt = $conn->prepare('SELECT * FROM adverts');
  $stmt->execute();
  $options = "";
  
  while($row = $stmt->fetch()) {
    $options .= "<option value='" . $row["advertid"] . "'>" . $row["adcontent"] . "</option>";
  }
  
  return $options;
}

function getbadwords($conn){
  $stmt = $conn->prepare('SELECT * FROM banned');
  $stmt->execute();
  $options = "";
  
  while($row = $stmt->fetch()) {
    $options .= "<option value='" . $row["id"] . "'>" . $row["word"] . "</option>";
  }
  
  return $options;
}

function deleteSMS($id, $conn){
  $stmt = $conn->prepare('DELETE FROM texts WHERE id = :id');
  $stmt->execute(array('id' => $id));
}

function deleteAdvert($id, $conn){
  $stmt = $conn->prepare('DELETE FROM adverts WHERE advertid = :id');
  $stmt->execute(array('advertid' => $id));
}


function removeBadword($id, $conn){
  $stmt = $conn->prepare('DELETE FROM banned WHERE id = :id');
  $stmt->execute(array('id' => $id));
}

switch($sender){
  case "newSMSForm":
    $content = $_POST['content'];
    $retVal = "A new SMS '" . $content . "' was submitted.";
    break;
  case "newAdvertForm":
    $advert = $_POST['newadvert'];
    $retVal = "Advert: '" . $advert . "' has been added to the database.";
    break;
  case "newBadWordForm":
    $badword = $_POST['newbadword'];
    $retVal = "Bad word: '" . $badword . "' has been added to the database.";
    break;
  case "removeSMSForm":
   $smsid = $_POST['smsid'];
   if (isset($smsid)){
     deleteSMS($smsid, $conn);
   }
   $retVal = getLastFiveSMS($conn);
}
echo $retVal;
break;

case "removeAdvert":
   $advertid = $_POST['advertid'];
   if (isset($advertid)){
     deleteAdvert($advertid, $conn);
   }
   $retVal = getadverts($conn);


echo $retVal;
break;

case "removeBadword":
   $badwordid = $_POST['id'];
   if (isset($badwordid)){
     deleteAdvert($badwordid, $conn);
   }
   $retVal = getbadwords($conn);
}

echo $retVal;
?>

Hi there,

I just checked your site and the errors I mention previously persist :frowning:

Anyway, we were 85% of the way there and I thought it was a shame to leave it at that, so I finished this off for you:

<!DOCTYPE HTML> 
<html> 
  <head> 
    <meta charset="utf-8"> 
    <title>SMS Display Admin</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 
    <style type="text/css"> 
      div {text-align: center}
      .results { margin-top: 15px; }
    </style> 
  </head>
  
  <body>
    <div data-role="page" id="admin" data-theme="b"> 
      <h2>SMS Display Admin</h2> 
      <div> 
        <a href="#newsms"><button data-inline="true">New SMS Message</button></a> 
        <a href="#removesms"><button data-inline="true">Remove a SMS</button></a> 
      </div> 
      <div> 
        <a href="#newadvert"><button data-inline="true">New Advert Message</button></a> 
        <a href="#removeadvert"><button data-inline="true">Remove a Advert</button></a> 
      </div> 
      <div> 
        <a href="#newbadword"><button data-inline="true">New Bad Word</button></a> 
        <a href="#removebadword"><button data-inline="true">Remove a badword</button></a> 
      </div> 
    </div> 
    
    <!--New SMS Page --> 
    <div data-role="page" id="newsms" data-theme="b"> 
      <div id="SMSinput"> 
        <form action="submit.php" method="post" id="newSMSForm" class="ajaxForm" data-action="new"> 
          <label for="newsms"><h2>SMS Input:</h2></label> 
          <input name="content" type="text" size="160" data-mini="true" id="newsms"/> 
          <input type="hidden" name="sender" value="4477777777777"> 
          <input type="hidden" name="inNumber" value="447786202125"> 
          <input type="hidden" name="email" value="none"> 
          <input type="hidden" name="credits" value="2501"> 
          <input type="hidden" name="forward" value="0"> 
          <input type="hidden" name="messagesent" value="0"> 
          <input name="newsms" data-role="button" data-inline="true" type="submit" value="submit" />
        </form>
        <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      </div> 
      <div class="results"></div>
    </div> 
    <!--End SMS Page -->
    
    <!--New Advert Page --> 
    <div data-role="page" id="newadvert" data-theme="b"> 
      <form action="submit.php" method="post" id="newAdvertForm" class="ajaxForm" data-action="new"> 
        <label for="newadvert"><h2>New Advert:</h2></label> 
        <input name="newadvert" type="text" size="160" id="newadvert" /> 
        <input name="newad" data-role="button" data-inline="true" type="submit" value="submit" /> 
      </form> 
      <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      <div class="results"></div>
    </div> 
    <!--End Advert Page --> 
    
    <!--New Bad Word Page --> 
    <div data-role="page" id="newbadword" data-theme="b"> 
      <form action="submit.php" method="post" id="newBadWordForm" class="ajaxForm" data-action="new"> 
        <label for="newbadword"><h2>New Bad Word:</h2></label> 
        <input name="newbadword" type="text" size="160" id="newbadword" /> 
        <input name="badword" data-role="button" data-inline="true" type="submit" value="submit" />
      </form> 
      <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      <div class="results"></div>
    </div> 
    <!--End Bad Word Page -->
    
    <!--Remove SMS--> 
    <div data-role="page" id="removesms" data-theme="b"> 
      <form action="submit.php"  method="post" id="removeSMSForm" class="ajaxForm" data-action="remove"> 
        <h2>SMS Removal</h2>
        <label for="smsid" class="select"><h2>Message:</h2></label> 
        <select name="smsid" id="smsid" data-mini="true"></select>        
        <input id="removeSmsButton" name="removesms" type="submit" value="submit" data-role="button" data-inline="true" data-theme="b" />
      </form> 
      <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      <div class="results"></div>
    </div> 
    <!--End Remove SMS-->  
    
    <!--Remove advert--> 
    <div data-role="page" id="removeadvert" data-theme="b"> 
      <form action="submit.php"  method="post" id="removeAdvertForm" class="ajaxForm" data-action="remove"> 
        <h2>Advert Removal</h2>
        <label for="advertid" class="select"><h2>Advert:</h2></label> 
        <select name="advertid" id="advertid" data-mini="true"></select>        
        <input id="removeAdvertButton" name="removeadvert" type="submit" value="submit" data-role="button" data-inline="true" data-theme="b" />
      </form> 
      <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      <div class="results"></div>
    </div> 
    <!--End Remove advert-->  
       
    <!--Remove Bad Word--> 
    <div data-role="page" id="removebadword" data-theme="b"> 
      <form action="submit.php"  method="post" id="removeBadwordForm" class="ajaxForm" data-action="remove"> 
        <h2>Bad Word Removal</h2>
        <label for="advertid" class="select"><h2>Advert:</h2></label> 
        <select name="badwordid" id="badwordid" data-mini="true"></select>        
        <input id="removeBadwordButton" name="removebadword" type="submit" value="submit" data-role="button" data-inline="true" data-theme="b" />
      </form> 
      <a href="#admin" class="back"><button data-inline="true">Back</button></a>
      <div class="results"></div>
    </div> 
    <!-- End Remove Bad Word-->  
       
    <script>
      (function(){
        function populateDropdown(values, menu, refresh){
          makeAjaxRequest(values, function(res){
            if (refresh){
              $('#' + menu).html(res).selectmenu("refresh")
            } else {
              $('#' + menu).html(res);
            }
          });
        }
        
        function getFormValues(form){
          var $inputs = $('#' + form + ' :input'), 
              values = {};
              
          $inputs.each(function() {
            values[this.name] = this.value;
          });
          
          return $.extend(values, {sender: form});
        }
        
        function makeAjaxRequest(values, callBack){
          $.ajax({
            type : "POST",
            url : "submit.php",
            data: values,
            success : function(res) {
              callBack(res);
            }
          });
        }
        
        function removeActiveButton(){
          $(".ui-btn-active").each(function(){
            $(this).removeClass("ui-btn-active");
          });
        }
        
        $(".ajaxForm").on("submit", function(){
          var values = getFormValues(this.id),
              action = $(this).data("action"),
              selectId = $(this).find("select").attr("id"),
              callBack;
              
          if(action === "new"){
            callBack = function(res){
              $(".results").html(res);
              $("input[type=text]").val("");
              removeActiveButton();
            }
          } else {
            callBack = function(res){
              alert("Operation successful!");
              populateDropdown(values, selectId, true);
              removeActiveButton();
            }
          }
          
          makeAjaxRequest(values, callBack)
          return false;
        });
        
        $(".back").on("click", function(){
          $(".results").html("");
        });
        
        populateDropdown({sender: "removeSMSForm"}, "smsid", false);
        populateDropdown({sender: "removeAdvertForm"}, "advertid", false);
        populateDropdown({sender: "removeBadwordForm"}, "badwordid", false);
      })()
    </script>
  </body>
</html>
<?php 
try {
  $conn = new PDO('mysql:host=localhost;dbname=test', 'root', '*****');
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo 'ERROR: ' . $e->getMessage();
}

$sender = $_POST['sender'];

function getLastFive($conn, $table){
  $stmt = $conn->prepare("SELECT * FROM $table WHERE content IS NOT NULL AND messagesent = 0 LIMIT 5");
  $stmt->execute();
  $options = "";
  
  while($row = $stmt->fetch()) {
    $options .= "<option value='" . $row["id"] . "'>" . $row["content"] . "</option>";
  }
  
  return $options;
}

function delete($conn, $table, $id){
  $stmt = $conn->prepare("DELETE FROM $table WHERE id = :id");
  $stmt->execute(array('id' => $id));
}

switch($sender){
  case "newSMSForm":
    $content = $_POST['content'];
    $retVal = "A new SMS '" . $content . "' was submitted.";
    break;
  case "newAdvertForm":
    $advert = $_POST['newadvert'];
    $retVal = "Advert: '" . $advert . "' has been added to the database.";
    break;
  case "newBadWordForm":
    $badword = $_POST['newbadword'];
    $retVal = "Bad word: '" . $badword . "' has been added to the database.";
    break;
  case "removeSMSForm":
    $smsid = $_POST['smsid'];
    if (isset($smsid)){
      delete($conn, "texts", $smsid);
    }
    $retVal = getLastFive($conn, "texts");
    break;
  case "removeAdvertForm":
    $advertid = $_POST['advertid'];
    if (isset($advertid)){
      delete($conn, "adverts", $advertid);
    }
    $retVal = getLastFive($conn, "adverts");
    break;
  case "removeBadwordForm":
    $badwordid = $_POST['badwordid'];
    if (isset($badwordid)){
      delete($conn, "badwords", $badwordid);
    }
    $retVal = getLastFive($conn, "badwords");
    break;
}

echo $retVal;

I have updated the JS logic to populate the menus and I have added two generic functions in the PHP to fetch the last five of anything, or to delete something depending on type and id (basically you pass the table as a variable to the function).

I hope this helps you.

Hi cheers for this much appreciated i need to have a look over it as all the remove buttons end up on the advert page and the dropdown’s are only filling with the texts and not their relative tables information.

i think it’s the mysql query so im going to try that out

the badword just required the label change

It’s the querys and the row names i think i’ve got this ill let you know.

chnaged the table row’s names and it appears to of resolved the issue.

i’ll add a function now to insert the table and hopefully all will go well :smiley:

after a few bits of mysql querys it’s all now working wonders

THANK YOU

for all your help!

No problem.
You’re very welcome :slight_smile: