views:

163

answers:

0

Using jquery alerts dialog plugin, how do I send a value in a form that is displayed in an alert, to a callback feature? The form is in the alert, and the callback happens after I click OK.

This is code that works, ignore this code as it is a working example, it basically shows how a form can be serialised. This code is taken from the Jquery documentation from the .serialise page. The problematic code goes below this.

<?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1'  "); ?><form id="changecategoryForm2"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select>  </form>   <p><tt id="results"></tt></p>


<script>
    function showValues() {
      var str = $("form#changecategoryForm2").serialize();
      $("#results").text(str);
    }
    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    showValues();
</script>

Compare that working code to the one I have with the Jquery alerts dialog plugin below the next code. Below is the default code to show an alert.

jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r) {
    if( r ) alert('You entered ' + r);
});

This is me putting the working html code of the form above into the alert, a type of javascript alert called a prompt.

The variable that I am trying to pass to the callback function is a serialised form called with the id of changecategoryForm.

  • Note that the working example above is a form called changecategoryForm*2*
  • The problematic form below is called changecategoryForm

Here is the problematic code.

$(".changecategory").click( function() {
   <?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1'  "); ?>
   var categoryid = $(this).attr("categoryid");
   var itemid = $(this).attr("itemid");
   var itemid2 = $(this).attr("itemid");
   var itemtitle = $(this).attr("itemtitle");
   var parenttag = $(this).parent().get(0).tagName;
   var removediv = "itemid_" +itemid;   
   jAlert(
      'Which category do you want to move <b>'+itemtitle+ '</b> to?<br><?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1'  "); ?><form id="changecategoryForm"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select>  </form>   <p><tt id="results"></tt></p>','Change category', function(r)    {
         // $(parenttag).fadeOut('slow');  
         $("#"+removediv).fadeOut('slow');  

         var newflex = $("#newflex").val();
         alert(newflex); // this shows nothing    
         alert($("#changecategoryForm").serialize());
         alert('categoryid ' + categoryid + ' itemid ' + itemid + '.'); //this shows the variables

         jAlert(''+ itemtitle +' has been successfully moved.', 'Success');
      } // this brace closes function(r)
   ); 
});