SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 73 of 73
  1. #51
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No Worries when i get a free moment ill have another look over the code to resolve it.

    got another project come up now but I am sure i can work it out

  2. #52
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am trying to change the array to a mysql_query ignore it's not a pdo query for now but any ideas how to resolve it.
    PHP Code:
    $sms =  mysql_query("SELECT * FROM `texts` WHERE `content` IS NOT NULL AND `messagesent` = 0 LIMIT 5");  //Grab 5 messages to display
            
    while ($row mysql_fetch_array($sms)) { 
            
            

    function 
    getLastFiveSMS(){
      global 
    $sms;
      
    $retVal "<option></option>";
      foreach (
    $sms as $k => $v){
        
    $retVal .= "<option value='" $k "'>" $v "</option>";
      }
      return 
    $retVal;


  3. #53
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    I would do it like this:

    Code:
    <?php 
    try {
      $conn = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
      $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;
    }
    
    function deleteSMS($id, $conn){
      $stmt = $conn->prepare('DELETE FROM texts 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;
    Does that help?

  4. #54
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi again,

    I just realised that when you click the buttons (to remove an SMS or whatever, they remain in a "pressed" state).
    I don't know if this is a bug in jQuery UI (similar bug report) or desired behaviour.
    You are however using a rather old version of jQuery UI, so I just wrote a function to sort this out.

    Here's the complete code:

    Code:
    <!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-->  
           
        <script>
          (function(){
            function populateDropdown(values){
              $.extend(values, {sender: "removeSMSForm"});
              makeAjaxRequest(values, function(res){
                $("#smsid").html(res).selectmenu("refresh");
              });
            }
            
            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"),
                  callBack;
                  
              if(action === "new"){
                callBack = function(res){
                  $(".results").html(res);
                  $("input[type=text]").val("");
                  removeActiveButton();
                }
              } else {
                callBack = function(res){
                  populateDropdown(values);
                  removeActiveButton();
                }
              }
              
              makeAjaxRequest(values, callBack)
              return false;
            });
            
            $(".back").on("click", function(){
              $(".results").html("");
            });
            
            populateDropdown({});
          })()
        </script>
      </body>
    </html>
    PHP Code:
    <?php 
    try {
        
    $conn = new PDO('mysql:host=localhost;dbname=test''user''password');
        
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::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;
    }

    function 
    deleteSMS($id$conn){
      
    $stmt $conn->prepare('DELETE FROM texts 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;
    I also updated the demo to work off of a database.
    I inserted 10 SMSs into the database and you can delete them from the drop down - try it out!

    HTH

  5. #55
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah cheers i can always update jquery that isnt a problem

  6. #56
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im going to have to host this site my self the current host (a friend) doesnt have the PDO extension installed got to love having a VPS

  7. #57
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've moved the site now but i am now adding functions for delete badword and advert.

  8. #58
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by txt3rob View Post
    i've moved the site now but i am now adding functions for delete badword and advert.
    Good stuff.
    Let me know if you run into any trouble.

  9. #59
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for the dropdown menu's do i need to change anything of the jquery as i had added the adverts in a similar fashion to the remove sms and it does not appear to dropdown.

  10. #60
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i implemented the following:

    PHP Code:
    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;

    i also added downbelow

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

    echo 
    $retVal

  11. #61
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by txt3rob View Post
    for the dropdown menu's do i need to change anything of the jquery as i had added the adverts in a similar fashion to the remove sms and it does not appear to dropdown.
    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.

  12. #62
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  13. #63
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code:
    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

    The console is showing:

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

  14. #64
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  15. #65
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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

  16. #66
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i have so far
    PHP Code:
    <?php 
    try {
        
    $conn = new PDO('mysql:host=localhost;dbname=xxx''xxx''xxx');
        
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::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;
    ?>

  17. #67
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

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

    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:

    Code:
    <!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 Code:
    <?php 
    try {
      
    $conn = new PDO('mysql:host=localhost;dbname=test''root''*****');
      
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::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.

  18. #68
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  19. #69
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think it's the mysql query so im going to try that out

  20. #70
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the badword just required the label change

  21. #71
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  22. #72
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after a few bits of mysql querys it's all now working wonders

    THANK YOU

    for all your help!

  23. #73
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,043
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    No problem.
    You're very welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •