AJAX Form Problem??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    AJAX Form Problem??

    I have this below code that calls an ajax page my problem is how do i pass the form values into the ajax page? I've tried $_REQUEST but it wont pick them up??

    Code:
    <form method="post" action="ajax-101-null/" enctype="multipart/form-data">
                    <div class="teamdb_line_wrapper">
                        <div class="teamdb_line_name">Group Name:</div>
                        <div class="teamdb_line_text"><input type="text" id="name" name="name" size="40" maxlength="80" /></div>
                    </div>
    </form>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    what do you mean with an 'AJAX-page'? an AJAX-call is made with the XMLHttpRequest-object and doesn't need a page or smething similar and you could pass values of the form-fields as params with the request ... have a look here for a basic AJAX-example

    kind regards

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      I'm just not sure how to explain my problem. I have the AJAX working fine. I click submit on the page and the relevant page opens but the $_REQUEST doesn't work on this page. It says that undefined variable?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        as i said ... when you make an AJAX-call to request something at the server you need to add the values to the request ... the form is not posted and so the values are not posted ...

        kind regards

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Thanks for that. I've added the values into the query using javascipt and it wil create a link like /ajax-101-name;23/
          Out of the name;23 string, the first bit is being displayed, but anything after the semi-colon is being stripped, I've tried other characters but i get a page forbidden error??

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            ?? i think i don't get what you did ... what is that link? and how did you pass the params to the request-object?

            kind regards

            Comment

            • Ciary
              Recognized Expert New Member
              • Apr 2009
              • 247

              #7
              by 'pass values of the form-fields as params with the request', gits means you need to add them to your URL. on the AJAX-page you'll be able to get them using $_GET

              example URL:
              Code:
              'yourpage.php?avariable='+document.getElementById('input').value+"&another="+document.getElementById('input2').value

              Comment

              • ziycon
                Contributor
                • Sep 2008
                • 384

                #8
                Originally posted by Ciary
                by 'pass values of the form-fields as params with the request', gits means you need to add them to your URL. on the AJAX-page you'll be able to get them using $_GET

                example URL:
                Code:
                'yourpage.php?avariable='+document.getElementById('input').value+"&another="+document.getElementById('input2').value
                I was using mod_rewrite which was causing issues with the link but got it sorted now. Thanks for the help.

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  While i have you here, is it possible to pass an image through a link? Or how would you go about it i this situation?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    what do you mean with pass an image? i guess you mean an upload? that needs to be done with a 'normal' file upload ...

                    kind regards

                    Comment

                    • ziycon
                      Contributor
                      • Sep 2008
                      • 384

                      #11
                      I know how to upload image through a normal form thats fine, I'm getting very confused when i try to do it with the way i have the ajax setup, any advice on how i would go about it?

                      Is this possible to upload an image with the way I'm using ajax at the moment?

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        hmmm ... i just don't have enough info about your 'setup' or what you mean with that or how you use AJAX at the moment ... may be this link is useful and answers your question?

                        kind regards

                        Comment

                        • ziycon
                          Contributor
                          • Sep 2008
                          • 384

                          #13
                          Everything works fine except i don't know how to go about getting the file uploaded??

                          This is my setup:
                          Code:
                          <div id="teamdb_container">
                                          <form method="post" onsubmit="getcontent(\'sys/ajaxCalls.php?acid=101&value=\'+document.getElementById(\'name\').value+\'-\'+document.getElementById(\'url\').value+\'-\'+document.getElementById(\'body\').value,\'teamdb_container\')">
                                          <div id="teamdb_title">/New Team</div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Group Name:</div>
                                              <div class="teamdb_line_text"><input type="text" id="name" name="name" size="40" maxlength="80" /></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Website:</div>
                                              <div class="teamdb_line_text"><input type="text" id="url" name="url" size="40" maxlength="80" /></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Banner:</div>
                                              <div class="teamdb_line_text"><input type="file" name="banner" size="50">
                                              <input type="hidden" name="MAX_FILE_SIZE" value="1000000"></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">About Us:</div>
                                              <div class="teamdb_line_text"><textarea id="body" name="body" rows="10" cols="60"></textarea></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_button"><input type="submit" value="Add New Team" /></div>
                                          </div>
                                          </form>
                                      </div>
                          Then in the ajaxCalls.php file i have:
                          Code:
                          if($acid == 101) {
                                    $value = explode('-',$value);
                                    if(($value[0]!='') && ($value[1]!='') && ($value[2]!='')) {
                                        //Query to add to db here
                                        echo '<div id="teamdb_container">
                                                <meta http-equiv="refresh" content="2;url=teamdb.html">
                                                <div id="db_add_successful">Team has been added successfully!</div>
                                                <div id="redirect_msgs">You will be redirected in 2 seconds...</div>
                                              </div>';
                                    }
                                    else {
                                        echo '<div id="teamdb_container">
                                          <form method="post" onsubmit="getcontent(\'sys/ajaxCalls.php?acid=101&value=\'+document.getElementById(\'name\').value+\'-\'+document.getElementById(\'url\').value+\'-\'+document.getElementById(\'body\').value,\'teamdb_container\')">
                                          <div id="teamdb_title">/New Team</div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Group Name:</div>
                                              <div class="teamdb_line_text"><input type="text" id="name" name="name" value="'.$value[0].'" size="40" maxlength="80" /></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Website:</div>
                                              <div class="teamdb_line_text"><input type="text" id="url" name="url" value="'.$value[1].'" size="40" maxlength="80" /></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">Banner:</div>
                                              <div class="teamdb_line_text"><input type="file" name="banner" size="50">
                                              <input type="hidden" name="MAX_FILE_SIZE" value="1000000"></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_name">About Us:</div>
                                              <div class="teamdb_line_text"><textarea id="body" name="body" rows="10" cols="60">'.$value[2].'</textarea></div>
                                          </div>
                                          <div class="teamdb_line_wrapper">
                                              <div class="teamdb_line_button"><input type="submit" value="Add New Team" /></div>
                                          </div>
                                          </form>
                                      </div>';
                                    }
                                }

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            did you have a look at the link i showed you above? there is an example for exactly that issue ...

                            Comment

                            • ziycon
                              Contributor
                              • Sep 2008
                              • 384

                              #15
                              Ye, can't seem to get my head round it, I'll have another look. thanks.

                              Comment

                              Working...