Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Form Trouble

  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts

    Form Trouble

    I'm not sure if this is a php issue or jquery issue. When I complete the form and hit submit it puts the successful message up like its supposed to but also with the fields still filled in the form which it shouldn't be doing and it doesn't actually post in the database so I'm not sure if its a php issue or jquery issue.


    Form
    Code:
    <script type="text/javascript">
        $(document).ready(function() {
        $('div.message-error').hide();
        $('div.message-success').hide();
        $("input.submit").click(function() {
            $('div.message-error').hide();
            var templatename = $("input#templatename").val();
      		if (templatename == "") {
            $("div.message-error").show();
            $("input#templatename").focus();
            return false;
          } 
          var headercode = $("textarea#headercode").val();
      		if (headercode == "") {
            $("div.message-error").show();
            $("textarea#headercode").focus();
            return false;
          } 
          var footercode = $("textarea#footercode").val();
      		if (footercode == "") {
            $("div.message-error").show();
            $("textarea#footercode").focus();
            return false;
          } 
          
            var dataString = 'templatename='+ templatename+ '&headercode=' + headercode + '&footercode=' + footercode;
            $.ajax({
            type: "POST",
            url: "processes/template.php",
            data: dataString,
            success: function() {
            $("div.message-success").show();
            return true;
        }
      });
      return false;    
        });
      });
      
    </script>
    <!-- Form -->
    <form action="#" name="templateform" >
    	<fieldset>
    		<legend>Add New Template</legend>
    
    		<div class="field required">
    			<label for="templatename">Template Name</label>
    			<input type="text" class="text" name="templatename" id="templatename" title="Template Name"/>
    			<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
    		</div>
            <div class="field required">
    			<label for="headercode">Header Code</label>
    			<textarea name="headercode" id="headercode" title="Header Code"></textarea>
    			<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
    		</div>
            <div class="field required">
    			<label for="footercode">Footer Code</label>
    			<textarea name="footercode" id="footercode" title="Footer Code"></textarea>
    			<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
    		</div>
            	<input type="submit" class="submit" name="submittemplate" id="submittemplate" title="Submit Template" value="Submit Template"/>
    	</fieldset>
    </form>
    <!-- /Form -->
    
    <!-- Messages -->
    <div class="message message-error">
        <h6>Required field missing</h6>
        <p>Please fill in all required fields. </p>
    </div>
    
    <div class="message message-success">
        <h6>Operation succesful</h6>
        <p>Template was added to the database.</p>
    </div>
    <!-- /Messages

    validation page
    PHP Code:
    <?php

    // Include the database page
    include ('inc/dbconfig.php');

    if ((isset(
    $_POST['templatename'])) && (strlen(trim($_POST['templatename'])) > 0)) {
        
    $templatename stripslashes(strip_tags($_POST['templatename']));
    } else {
    $templatename 'No name entered';}

    if ((isset(
    $_POST['headercode'])) && (strlen(trim($_POST['headercode'])) > 0)) {
        
    $headercode stripslashes(strip_tags($_POST['headercode']));
    } else {
    $headercode 'No name entered';}

    if ((isset(
    $_POST['footercode'])) && (strlen(trim($_POST['footercode'])) > 0)) {
        
    $footercode stripslashes(strip_tags($_POST['footercode']));
    } else {
    $footercode 'No name entered';}

     
    $query "INSERT INTO `templates` (templatename, header, footer, creator_id, datecreated) VALUES ('".$divisionname."','".$headercode."','".$footercode."' 1, NOW())";
     
    mysql_query($query);

    ?>
    Last edited by CoolAsCarlito; 11-25-2010 at 06:27 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,187
    Thanks
    15
    Thanked 253 Times in 253 Posts
    The isset needs to be applied to the submit button name for the php and not the name of the form. However the form name is useful for the javascript.

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    I'm confused. Is any of those issets correct?

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,187
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by CoolAsCarlito View Post
    I'm confused. Is any of those issets correct?
    You should have an isset for the submit button, I'm not even sure that the other items can be tested that way. Normally they would not be tested with conditional statements until the form is submitted. You can test them with javascript before the form is submitted and enable the submit button if it passes the tests. Or jQuery may have it's own methods. If you submit the form to the server the fields will go blank unless you have some way to reload them.

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Someone told me I should redo it and this is what I came up with instead for the validate form. I'm just curious to see if I have any tags wrong or parenthesis wrong also would like to know in fact this should work in theory.

    The reason why I don't have mysql_real_escape_string for the header and footer codes is because that will have html coding inside of it and only admins can fill out this form. So I'm not too worried about it right now.

    But other than that is everything alright?

    PHP Code:
    <?php

    // Include the database page
    include ('inc/dbconfig.php');

    if (isset(
    $_POST['submittemplate'])) {
        
    $templatename mysql_real_escape_string($_POST['templatename']);
        
    $headercode $_POST['headercode'];
        
    $footercode $_POST['footercode'];
        
        
    $query "INSERT INTO `templates` (templatename, header, footer, creator_id, datecreated) VALUES ('".$templatename."','".$headercode."','".$footercode."', 1, NOW())";
        
    mysql_query($query);
    }
            
    ?>

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,187
    Thanks
    15
    Thanked 253 Times in 253 Posts
    That certainly looks like it should work with a standard form, one without php or javascript preloading the form fields. I don't see any basic syntax errors.

  • #7
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Sounds good to me. Thank you now I need to find out withe the ajax/jquery why its not working.


  •  

    Posting Permissions

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