SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help passing form variables through javascript

    Hi, I downloaded this comment wall script here Facebook Wall Script 3.0 with PHP and Jquery

    And I'm attempting to customise and expand it and I've not done so bad so far. Guy knows what he is doing but I've managed to condense a lot of functions and better organise some stuff for better results.

    But I cannot figure out the javascript side. I want to require a poster to put in their name and email before submitting a comment. I struggled to figure out why the variables weren't passing and then realised that they were processed through a javascript file for AJAX. But I cannot understand it well enough to figure out how to pass the additional variables.

    This is the code.
    Code JavaScript:
    $(document).ready(function() 
    {
    // Update Status
    $(".update_button").click(function() 
    {
    var updateval = $("#update").val();
    var dataString = 'update='+ updateval;
    if(updateval=='')
    {
    alert("Please Enter Some Text");
    }
    else
    {
    $("#flash").show();
    $("#flash").fadeIn(400).html('Loading Update...');
    $.ajax({
    type: "POST",
    url: "message_ajax.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#flash").fadeOut('slow');
    $("#content").prepend(html);
    $("#update").val('');	
    $("#update").focus();
     
     
     
    $("#stexpand").oembed(updateval);
      }
     });
    }
    return false;
    	});

    There's further code in the file but this is the part pertinent to what I need. updateval, as far as I can tell is taking the information from "update" here.
    Code HTML4Strict:
    <form method="post" action="">
     
         <input type='text' name='nickname' id='nickname' maxlength='25' />
    		<label for='fullname'>Your Name: <em>(required)</em> </label>
         	<br />													
         		<input type='email' name='email' id='email' maxlength='30'/>			
    	 			<label for='email'>Email (will not be published) <em>(required)</em> </label>
         			<br />													
         				<input type='text' name='website' id='website' maxlength='50'/>			
    	 					<label for='website'>Website </label>
               			 	<br />	
    							<textarea cols="30" rows="4" name="update" id="update" maxlength="400" ></textarea>
                    			<br />
     
    <input type="submit" value=" Submit Comment "  id="update_button"  class="update_button"/>
    </form>

    Does anyone know how to do this? I'm sure it will be something simple but I just don't know any javascript or AJAX, all I ended up doing was combining username and message into one field.

    If it helps, this is what I've done. http://cms.mediaxombie.com/wall/

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    If you want to post their name and email address as a part of the ajax request, you will need to update this piece of the code:

    Code javascript:
    var updateval = $("#update").val();
    var dataString = 'update='+ updateval;

    Here is where the sanity check is performed:

    Code javascript:
    if (updateval=='') {
    alert('Please Enter Some Text');
    } else {
        ...
    }

    So you can add other conditions as an "else if" part of the structure.

    Code javascript:
    if (updateval=='') {
        alert("Please Enter Some Text");
    } else if (...) {
        ...
    } else if (...) {
        ...
    } else {
        ...
    }

    where the else if conditions check for their name and email address.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah that's what I want, to pass it to message_ajax.php

    I have the variables passing INTO the javascript but I want to pass them on. But when I tried to alter this line:
    Code JavaScript:
    var dataString = 'update='+ updateval;

    It just came out as a single string. Is there some trick to keeping them as separate items or a special icon to divide them?

    I tried
    Code JavaScript:
    var dataString = 'update='+ updateval;'nickname='+updateval2

    and so on but that only showed the first value, so I'm obviously not knowledgeable on this.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by darkwarrior View Post
    but that only showed the first value, so I'm obviously not knowledgeable on this.
    Do you know about the structure of a querystring?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean something like this?

    Code JavaScript:
    var dataString = 'update='+ updateval & 'nickname='+updateval2;

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by darkwarrior View Post
    Do you mean something like this?

    Code JavaScript:
    var dataString = 'update='+ updateval & 'nickname='+updateval2;
    Something like that. The ampersand should be in the string as an ordinary piece of text
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YESSSSSSSSSSSSSSSSSSSSSSS

    Thank you so much paul, I was quite chuffed with how I'd developed his code but couldn't get this at all. Never would have thought to put the "&" in the string.

    Thanks a lot for the help.


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
  •