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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Help with adding code to submit button please!!

    Hi all,

    i have a div which includes a button, i want this button to display a message in my div if the validation is correct and submit some data into my database and if the validation is incorrect it displays an error message within the div.

    as far as i am aware a form button can only sumit either a get or post which directs to a new page? if i had a button minus the form can i add some custom code to do what i want aove?

    cheers
    Luke
    Last edited by LJackson; 03-05-2009 at 03:44 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    This is a PHP question. Nothing to do with JavaScript. So I've asked moderator to move it.

    Since the form *MUST* submit to the PHP code in order for the captcha value to be tested, you can't do any of this in JavaScript.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ooops sorry, thanks for asking it to be moved
    Last edited by LJackson; 03-04-2009 at 02:52 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    can someone move it to the php section please

    thanks
    Last edited by LJackson; 03-04-2009 at 07:05 PM.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks guys for moving it for me

    can anyone help me with this please?

    cheers
    Luke

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    anyone!!!!! PLEASE

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by LJackson View Post
    Hi all,

    i have a div which includes a button, i want this button to display a message in my div if the validation is correct and submit some data into my database and if the validation is incorrect it displays an error message within the div.

    as far as i am aware a form button can only sumit either a get or post which directs to a new page? if i had a button minus the form can i add some custom code to do what i want aove?

    cheers
    Luke
    As far as I know I believe you will have to use GET and POST with a form to accomplish what you desire. Here is the basic code for something on par with what you are wanting.

    Code:
    <?php	
       $val = $_GET['val']; //use GET to retrieve val
    	if(empty($_POST)){
    		//Check to see if $val is empty.
    		if(!empty($val)){
    			$status = 'Information added.';			
    		}else{
    			$status = 'To add information, fill out the form below. Click the Submit button once.';
    		}		
    	}else{
    		$info=$_POST['info']; //use POST to fill $info with information submitted in the form.		
    			
    		$sql="INSERT INTO table "; //table should be changed to the name of your table
    		$sql.="SET info='$info' ";	
    									
    		if(mysql_query($sql)){
    			header('Location:thispage.php?val=yes'); //Upon successfully Inserting information into the table takes user to thispage.php?val=yes which passes yes through val to show the user that information was successfully submitted. Change thispage.php to the current pages url.
    		}else{
    			$status = 'Unable to add information.'; //Upon unsuccessful insertion of information into the table.
    		}
    	}
    ?>
    
    <div><?php echo $status; ?></div> <!-- displays information based on current status -->
    <form enctype="multipart/form-data" action="thispage.php" method="post"> <!-- POST form that returns user to this page -->
    	<dl>
    		<dt><label for="info">Insert information.</label></dt>
    		<dd><input type="text" name="info" id="info" value="" /></dd> <!-- user supplies info to be submitted -->	
    	</dl>
    	<div>
    		<input type="submit" name="submit" value="Submit" /> <!-- Upon depression will submit form -->
    	</div>
    </form>
    That is the basic code for doing what you were asking using GET, POST, and a form. Not sure it is what you were looking for though.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi mate,

    thanks for this, i "did" have something almost working before i wrecked it

    now i am having quite a few problems with it all

    i will persevere with it and hopefully get it working

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by LJackson View Post
    Hi mate,

    thanks for this, i "did" have something almost working before i wrecked it

    now i am having quite a few problems with it all

    i will persevere with it and hopefully get it working
    Not a problem. Let me know if you require further assistance.

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    will do cheers mate

  • #11
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    what?! why can't he do this in javascript? if he's using ajax this is completely possible.

    take your submit button and change type from "submit" to "button". add an onclick event that starts up a validator function.

    if the validator sees that the input meets your requirements, it could fire an ajax request which could take the response and generate any output you want based on the results. if the validator says the input fails your requirements it would output the error of your choice.


    or if you wanted you could have the submit button remain as is, but stay disabled until the validator says everything is cool. you would use the onkeypress event on your form to monitor the input.

  • #12
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ohgod View Post
    what?! why can't he do this in javascript? if he's using ajax this is completely possible.

    take your submit button and change type from "submit" to "button". add an onclick event that starts up a validator function.

    if the validator sees that the input meets your requirements, it could fire an ajax request which could take the response and generate any output you want based on the results. if the validator says the input fails your requirements it would output the error of your choice.


    or if you wanted you could have the submit button remain as is, but stay disabled until the validator says everything is cool. you would use the onkeypress event on your form to monitor the input.
    Sounds like that would work but I am fairly new to javascript and ajax so wouldn't know for certain. Sounds like you may be a better help to him than I was.

  • #13
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by ohgod View Post
    what?! why can't he do this in javascript? if he's using ajax this is completely possible.

    take your submit button and change type from "submit" to "button". add an onclick event that starts up a validator function.

    if the validator sees that the input meets your requirements, it could fire an ajax request which could take the response and generate any output you want based on the results. if the validator says the input fails your requirements it would output the error of your choice.


    or if you wanted you could have the submit button remain as is, but stay disabled until the validator says everything is cool. you would use the onkeypress event on your form to monitor the input.
    hi mate, i know nothing about javascript or ajax so this is going to be difficult for me , i have changed my button type to button and have added an onclick even but im not sure where to go next?

    ideally i would want it to check the image validator to see if it has been inserted correctly and then display a message if it has/hasnt.

    here is my form code
    Code:
    <form name="rate" method="post">
    <div id="apDiv1">
        <table width="294" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="167"><span class="ratingscat_head">Value for money</span></td>
            <td width="119"><span class="storeRatings_Individual"><?php print rating_bar('valuetemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">Ease of use</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('easeofusetemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">Delivery time</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('deliverytemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">P&amp;P costs</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('delchargetemp',5)?></span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><label for="label"><span class="style2">Please Enter Security Code: </span></label></td>
          </tr>
          <tr>
            <td colspan="2"><input id="security_code" name="security_code" type="text" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><input type="button" name="button" value="button" onclick=""/></td>
            <td><a href="javascript:dropdowncontent.hidediv('subcontent')">Close</a></td>
          </tr>
        </table>
    <P>
        			</form>
    i have some code to check the image validation
    PHP Code:
        if( isset($_POST['submit'])) {
               if( 
    $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
            
    // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
        
            
            
    $message="Thank you for voting!!!";

            unset(
    $_SESSION['security_code']);
       } else {
            
    // Insert your code for showing an error message here
            
    $message="Sorry, you have provided an invalid security code";
       }
    } else {} 
    can it be incorporated into the onclick or another way? does this sound possible?
    cheers mate
    Luke
    Last edited by LJackson; 03-06-2009 at 04:22 PM.

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by Serihon View Post
    Sounds like that would work but I am fairly new to javascript and ajax so wouldn't know for certain. Sounds like you may be a better help to him than I was.
    hi mate, i dont know anything about javascript and ajax so you are better off than me mate thanks for your help so far please keep an eye on this thread im sure your wisdom will be useful again at some point

    cheers mate
    Luke

  • #15
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi all,

    ok i have learnt a bit of js

    i have a file which i have called jsfunctions.js and this contains
    PHP Code:
    <script language="javascript" type="text/javascript">
    function 
    showAlert() {
     
    alert('Hello World - this is an alert message!');
    }
    </script> 
    as a tester

    and then i have added this to my button
    Code:
    <input type="Button" name="Button" value="Button" onclick="showAlert();"/>
    and this shows the alert when i click on the button

    so im getting somewhere

    but im still not sure how to add the img checker to the onclick, can i just copy and paste it into my jsfunctions page?

    cheers
    Luke

    edit
    ===

    i tried adding my php checker code to my js function and nothing how do you add php into js? can you do it?

    here is my code
    Code:
    function checkimg(){
    if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
    		// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
    $sql="UPDATE ratings 
      SET 
        used_ips = NULL,
        total_value = 0,
        total_votes = 0 
      WHERE 
      id IN ('valuetemp', 'easeofusetemp', 'deliverytemp', 'delchargetemp')";
      $query = mysql_query($sql);		
    		
    		alert('Your vote has been counted! Thank you for voting');
    		unset($_SESSION['security_code']);
       } else {
    		// Insert your code for showing an error message here
    		alert('Sorry, you have provided an invalid security code');
       }
    }
    Last edited by LJackson; 03-06-2009 at 05:25 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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