javascript/ajax to mysql db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #31
    Originally posted by anfetienne
    ok i understand on how to select the fields i need but i don't understand where to put that in the code
    Before you open the request (before line 28 in post #11), then change 'GET' to 'POST':
    Code:
    httpRequest.open('POST',url,true);

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #32
      Originally posted by acoder
      For the fields, either add an ID so you can use document.getEle mentById() to access them or use:
      Code:
      document.getElementById("form").elements[name-of-field].value
      To make a POST request (use the send() method), construct the field-value parameters, e.g.
      Code:
      var urlparams = "first_name=" + encodeURIComponent(document.getElementById("first_name").value);
      urlparams += "&last_name=" + encodeURIComponent(document.getElementById("first_name").value);
      // and so on...
      now i understand what you done here....i think i get all of it almost......las t thing is where do i place this in my ajax code?

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #33
        Thanks in advance everyone....bee n a great help!!!!

        Comment

        • Ciary
          Recognized Expert New Member
          • Apr 2009
          • 247

          #34
          if you did this with GET you would have done this

          Code:
          var url = "file.php"
          url += "?firstname="+document.getElementById("form").elements['first_name'].value
          url += "&lastname="+document.getElementById("form").elements['last_name'].value
          url += "&city="+document.getElementById("form").elements['city'].value
          
          //... you have to do all your fields like this
          httpRequest.open('GET',url,true);

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #35
            Originally posted by anfetienne
            last thing is where do i place this in my ajax code?
            Before the onreadystatecha nge line.

            Comment

            • anfetienne
              Contributor
              • Feb 2009
              • 424

              #36
              ok final question lol...thanks again for the help though......say i put all this coding in a file index.php and i wanted it to do everything within this file would i call the file at the end index.php? and would it be best to use the if and else statement in php to run the sql once ajax has done its part?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #37
                It might be easier to keep it in separate files, but if you do want it in one file, then yes, you would need to use if-else to ensure that the correct part is run, e.g. by checking the existence of one of the POST-ed values.

                Comment

                • anfetienne
                  Contributor
                  • Feb 2009
                  • 424

                  #38
                  ok got it....thanks for all the help im going to be testing tomorrow so ill let you know the results then

                  Comment

                  • anfetienne
                    Contributor
                    • Feb 2009
                    • 424

                    #39
                    i forgot to ask....what is it i put to run onmouseover or onsubmit etc?

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #40
                      Do you mean your validation function? It should run onsubmit, but if you're using Ajax to submit the form, you could just call the validation function within the makeRequest() function.

                      Comment

                      • anfetienne
                        Contributor
                        • Feb 2009
                        • 424

                        #41
                        ok ive tested the code and it works the only problem i'm having is that it won't run the code without opening another page. So far i have only got it to work when the form submission looks like this

                        <form action="" method="post" target="paypal" id="form">

                        this opens a new window and it does as it should then but i can't get it to run in the same window

                        Comment

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #42
                          i think this is where if and if else would come in actually....i know this should be in the php forum but as you mentioned it how do you do a if statement on $_POST

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #43
                            You can use isset to check if a variable is set.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #44
                              Originally posted by anfetienne
                              ok ive tested the code and it works the only problem i'm having is that it won't run the code without opening another page.
                              Can you post the latest version of your code.

                              Comment

                              • anfetienne
                                Contributor
                                • Feb 2009
                                • 424

                                #45
                                this is my code

                                Code:
                                <script type="text/javascript" language="javascript">
                                    function makeRequest(url) {
                                        var httpRequest;
                                
                                        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                                            httpRequest = new XMLHttpRequest();
                                            if (httpRequest.overrideMimeType) {
                                                httpRequest.overrideMimeType('text/xml');
                                                // See note below about this line
                                            }
                                        } 
                                        else if (window.ActiveXObject) { // IE
                                            try {
                                                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                                            } 
                                            catch (e) {
                                                try {
                                                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                                                } 
                                                catch (e) {}
                                            }
                                        }
                                
                                        if (!httpRequest) {
                                            alert('Giving up :( Cannot create an XMLHTTP instance');
                                            return false;
                                        }
                                		
                                		//enter var details for fields here
                                		var urlparams = "first_name=" + encodeURIComponent(document.getElementById("first_name").value);
                                		urlparams += "&last_name=" + encodeURIComponent(document.getElementById("last_name").value);
                                		urlparams += "&address1=" + encodeURIComponent(document.getElementById("address1").value);
                                		urlparams += "&address2=" + encodeURIComponent(document.getElementById("faddress2").value);
                                		urlparams += "&city=" + encodeURIComponent(document.getElementById("city").value);
                                		urlparams += "&state=" + encodeURIComponent(document.getElementById("state").value);
                                		urlparams += "&zip=" + encodeURIComponent(document.getElementById("zip").value);
                                		urlparams += "&os0=" + encodeURIComponent(document.getElementById("os0").value);
                                		urlparams += "&email=" + encodeURIComponent(document.getElementById("email").value);
                                
                                		
                                        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
                                        httpRequest.open('POST', url, true);
                                        httpRequest.send('urlparams');
                                
                                    }
                                
                                    function alertContents(httpRequest) {
                                
                                        if (httpRequest.readyState == 4) {
                                            if (httpRequest.status == 200) {
                                                alert(httpRequest.responseText);
                                            } else {
                                                alert('There was a problem with the request.');
                                            }
                                        }
                                
                                    }
                                </script>
                                and this is the form....i have put the paypal link as a php comment so you can see the address the form submits to

                                Code:
                                <table width="675" border="0" align="center" cellpadding="0" cellspacing="0">
                                                          <tr><? //https://www.paypal.com/cgi-bin/webscr?>
                                                            <td><form action="" method="post" target="paypal" id="form">
                                                                <p>
                                                                  <input type="hidden" name="on0" value="shipping To:" />
                                                              </p>
                                                              <table width="675" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                  <tr>
                                                                    <td height="825" background="images/leadBox.jpg"><table width="550" height="133" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                        <tr>
                                                                          <td height="40"><div align="center"><span class="style112">All fields marked with <span class="style114">*</span> are required and must be entered before continuing to the secured paypal payment page.</span></div></td>
                                                                        </tr>
                                                                        <tr>
                                                                          <td height="25">&nbsp;</td>
                                                                        </tr>
                                                                        <tr>
                                                                          <td><table width="490" border="0" cellspacing="0" cellpadding="0">
                                                                              <tr>
                                                                                <td height="28" bgcolor="#666666"><span class="style118">...</span><span class="style116">Product Order</span></td>
                                                                              </tr>
                                                                              <tr>
                                                                                <td><table width="550" border="1" cellspacing="0" cellpadding="0">
                                                                                    <tr>
                                                                                      <td width="71" height="25" bgcolor="#CCCCCC" class="style96"><div align="center"><span class="style120">Quantity</span></div></td>
                                                                                      <td width="407" bgcolor="#CCCCCC" class="style96"><div align="center"><span class="style120">Description</span></div></td>
                                                                                      <td width="64" bgcolor="#CCCCCC" class="style96"><div align="center"><span class="style120">Price</span></div></td>
                                                                                  </tr>
                                                                                    <tr>
                                                                                      <td><div align="center" class="style121">1</div></td>
                                                                                      <td height="50"><table width="400" height="28" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                                          <tr>
                                                                                            <td><div align="left"><span class="style149">Adam Ginsberg’s ‘The Closely Guarded Secrets Of An eBay  Millionaire – Make Money On eBay’</span></div></td>
                                                                                          </tr>
                                                                                      </table></td>
                                                                                      <td height="30"><div align="center" class="style121">
                                                                                        <p>£97.00<br />
                                                                                        (inc VAT)</p>
                                                                                        </div></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                      <td height="32">&nbsp;</td>
                                                                                      <td><table width="400" height="28" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                                          <tr>
                                                                                            <td><div align="left">
                                                                                                <p class="style121">Postage &amp; Packaging:<br />
                                                                                                  <strong>Calculated via Royal Mail according to Country 
                                                                                              of Residence</strong></p>
                                                                                            </div></td>
                                                                                          </tr>
                                                                                      </table></td>
                                                                                      <td height="32">&nbsp;</td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                      <td height="23" bgcolor="#CCCCCC">&nbsp;</td>
                                                                                      <td bgcolor="#CCCCCC"><div align="right" class="style123">Sub-Total<span class="style124">..</span></div></td>
                                                                                      <td bgcolor="#CCCCCC"><div align="center" class="style123">£97.00</div></td>
                                                                                    </tr>
                                                                                </table></td>
                                                                              </tr>
                                                                          </table></td>
                                                                        </tr>
                                                                        <tr>
                                                                          <td height="28" bgcolor="#FFFFFF">&nbsp;</td>
                                                                        </tr>
                                                                        <tr>
                                                                          <td height="28" bgcolor="#666666"><span class="style118">...</span><span class="style116">Billing Information</span></td>
                                                                        </tr>
                                                                      </table>
                                                                        <table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                          <tr>
                                                                            <td bgcolor="#CCCCCC"><table width="475" border="0" align="center" cellpadding="2" cellspacing="2">
                                                                                <tr>
                                                                                  <td>&nbsp;</td>
                                                                                  <td>&nbsp;</td>
                                                                                  <td>&nbsp;</td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td><div align="left"><span class="style123">First Name: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="first_name" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td><div align="left"><span class="style123">Last Name: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="last_name" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">Address Line 1: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="address1" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">Address Line 2: </span></div></td>
                                                                                  <td>&nbsp;</td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="address2" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">City: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="city" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">County/State/Province: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="state" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">Postal Code/Zip Code: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <input type="text" name="zip" value="" />
                                                                                  </div></td>
                                                                                </tr>
                                                                                <tr>
                                                                                  <td class="style123"><div align="left"><span class="style96">Country: </span></div></td>
                                                                                  <td><span class="style112"><span class="style114">*</span></span></td>
                                                                                  <td><div align="left">
                                                                                      <select name="os0" id="os0">
                                                                                        <option value="">Please Select ...</option>
                                                                                        <option value="Albania">Albania</option>
                                                                                        <option value="Algeria">Algeria</option>
                                <!-- removed for better readability -->
                                                                                        <option  value="Yemen">Yemen</option>
                                                                                        <option  value="Zambia">Zambia</option>
                                                                                      </select>
                                                                                  </div></td>
                                                                                </tr>
                                                                                
                                                                                <tr>
                                                                                  <td class="style123"><div align="left">Email Address</div></td>
                                                                                  <td><span class="style150">*</span></td>
                                                                                  <td><div align="left">
                                                                                    <input type="text" name="email" value="" id="email" />
                                                                                  </div></td>
                                                                                </tr>
                                                                            </table></td>
                                                                          </tr>
                                                                          <tr>
                                                                            <td height="50" bgcolor="#CCCCCC"><div align="center">
                                                                                <input name="submit" type="image" onclick="CalculateOrder(this.form)" onmouseover="validate(this.form)" onsubmit="makeRequest('')" src="https://www.theauctionwinners.com/main/payment/images/paymentBtn.gif" alt="Continue To PayPal" width="152" height="50" border="0" />
                                                                                <input type="hidden" name="upload" value="1" />
                                                                                <input type="hidden" name="cmd" value="_cart" />
                                                                                <input type="hidden" name="business" value="john@future-resourcings.com" />
                                                                                <input type="hidden" name="item_name_1" value="Adam Ginsberg's The Closely Guarded Secrets Of An Ebay Millionaire - Make Money On eBay - FAST TRACK DVD Home Study Course (Inc VAT)" />
                                                                                <input type="hidden" name="quantity_1" value="1" />
                                                                                <input type="hidden" name="amount_1" value="97.00" />
                                                                                <input type="hidden" name="shipping_1" />
                                                                                <input type="hidden" name="item_number_1" />
                                                                                <input type="hidden" name="country" />
                                                                                <input type="hidden" name="currency_code" value="GBP"/>
                                                                                <input type="hidden" name="no_shipping" value="2" />
                                                                                <input type="hidden" name="return" value="http://www.theauctionwinners.com/main" />
                                                                                <input type="hidden" name="bn" value="TheAuctionWinners_ShopCart_WPS_GB" />
                                                                                <!-- Enable override of payer’s stored PayPal address. -->
                                                                                <input type="hidden" name="address_override" value="1" />
                                                                            </div></td>
                                                                          </tr>
                                                                          <tr>
                                                                            <td bgcolor="#FFFFFF"><div align="center"><span class="style125">I authorise Future Resourcings Ltd to charge me for the above sub-total and forthcoming postage &amp; package.
                                                                              I further affirm that the personal information provided on this form are true and correct.</span></div></td>
                                                                          </tr>
                                                                        </table>
                                                                      <table width="220" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                          <tr>
                                                                            <td>&nbsp;</td>
                                                                          </tr>
                                                                          <tr>
                                                                            <td><div align="center"><strong class="style112">By continuing to PayPal,<br />
                                                                              I agree to pay Future Resourcings Ltd.</strong></div></td>
                                                                          </tr>
                                                                        </table>
                                                                      <table width="300" height="38" border="0" align="center" cellpadding="0" cellspacing="0">
                                                                          <tr>
                                                                            <td>&nbsp;</td>
                                                                          </tr>
                                                                      </table></td>
                                                                </tr>
                                                                </table>
                                                              </form></td>
                                                          </tr>
                                                        </table>
                                Last edited by Dormilich; May 8 '09, 05:42 AM. Reason: removed some lines for better readability

                                Comment

                                Working...