SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to send current language with jquery ajax to php script?

    For a multilingual website I am using translation files which I have included in the different pages. The different translation files look similar to this:
    Code:
    <?php
    $name       = "name";
    $email        = "email";
    $tel             = "telephone";
    $sub           = "subject";
    $mes          = "message";
    $btn            = "send";
    
    $salutation  = "Dear";
    $message   = "
    Your message has been successfully sent and we will contact you as soon as possible If you have not received a response from us within 24 hours please call us.";
    $signature   = "Management & Staff Fan's Restaurant.";
    ?>
    The first 6, which I use for placeholders in my contact.php (included on the main contact page), are working fine. But the last 3, which I need in in the process_contact_form.php file are not working. I call the processing page true a ajax call:
    Code:
                $.ajax({
                      type: "POST",
                      url: "../modules/site/process_contact_form.php",
                      data: dataString,
                      cache: false,
    What could be the reason that those last three are not working. Could it be be because process_contact_form.php is included in a file contact.php, which is by it self included as well. Should I store them as global variables and if so how should I do that?

    Thank you in advance
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What do you mean by "not working"?
    The PHP code you posted is just a few variables with their values.
    If you call that through AJAX, nothing will be returned.

  3. #3
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido. Thank you for your responce. So how do I pass those variable (stored in translations_en.php) to process_contact_form.php which is indeed called true AJAX?
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Do an include of translations_en.php in process_contact_form.php ?

  5. #5
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido. I was thinking of that. The problem I have is that all files, except for the default ones (dutch) are located in their own directory (en/fanskitchen-contact.php, de/fanskitchen-contact.php, it/fanskitchen-contact.php, etc). Sow when I am on the Italian contact page: it/fanskitchen-contact.php. translations_it.php should be included and when I am on the English one it/fanskitchen-contact.php, translations_en.php should be included etc. How should I approach that.
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You'll have to send the current language to the PHP script through the ajax request, and then use that in the script to include the correct language file.

  7. #7
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido. And how should I send the current language? Or should I continue this question in the Javascript forum?
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by donboe View Post
    Hi Guido. And how should I send the current language? Or should I continue this question in the Javascript forum?
    If you don't know how to send variables through a jquery ajax call, then yes, you should ask that question in the JS forum.
    But since this thread hasn't really solved anything yet, I'm just going to move it there

  9. #9
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido.Thank youfor relocating this threat to the Javascript forum.

    The situation:I pass certain form values with a AJAX call the following way:
    Code:
    var dataString = '$name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment;
    $.ajax({
      type: "POST",
      url: "../modules/site/process_contact_form.php",
      data: dataString,
      cache: false,
      beforeSend: function(){
        $("#send_status").html('<div style="padding-left:100px;"><img src="loading.gif" alt="Sending...." align="absmiddle" title="Sending...."/> <font style="font-family:Verdana, Geneva, sans-serif; font-size:11px; color:black;">Please wait...</font></div><br clear="all">');
        },
        success: function(response){
          $("#send_status").html($(response).fadeIn(2000));
          $(".contL").fadeOut(1000);
          $("#name").val('');
          $("#email").val('');
          $("#phone").val('');
          $("#subject").val('');
          $("#comment").val('');
    						
        }
    });
    Next to the form values i need to send the following 3 PHP variables to process_contact_form.php
    Code:
    $salutation  = "Dear";
    $message   = "
    Your message has been successfully sent and we will contact you as soon as possible If you have not received a response from us within 24 hours please call us.";
    $signature   = "Management & Staff Fan's Restaurant.";
    ?>
    How do I add those 3 variables to the above AJAX call.

    Thank you in advance!!!
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  10. #10
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi donboe,

    Quote Originally Posted by donboe View Post
    How do I add those 3 variables to the above AJAX call
    You can't.

    PHP is interpreted on the server and the resultant HTML is sent to the browser.
    It is only the HTML that you can manipulate.
    JS has no concept of PHP variables.

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Ok, what I think you should do is:

    AJAX call:
    Code:
    var dataString = '$name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment + '&language='+ language;
    Where the JS variable 'language' would contain the current language.

    Then in the process_contact_form.php, you include the correct language file using the selected language:
    PHP Code:
    // sanitize and validate the variables sent by the ajax call
    $language whateveryouneedtodotosanitizeandvalidate($_POST['language']);
    $languagefile 'translations_' $language '.php';
    include 
    $languagefile

  12. #12
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido. I think I understand the principle what you want to accomplish. But after I added + '&lang='+ lang (langis the variable I use to determine the language) to var datastring the form wasn't proccessed any longer. By the way this is the entire Javascrip function:
    Code:
    function submit_contact_form(){
          var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
          var name = $("#name").val();
          var email = $("#email").val();
          var phone = $("#phone").val();
          var subject = $("#subject").val();
          var comment = $("#comment").val();
    
          if(name == "")
          {
                $("#send_status").html('<div class="error" style="color: #C00">Vul uw naam in om verder te gaan.</div>');
                $("#name").focus();
          }
          else if(email == "")
          {
                $("#send_status").html('<div class="error" style="color: #C00">Vul uw e-mailadres in om verder te gaan.</div>');
                $("#email").focus();
          }
          else if(reg.test(email) == false)
          {
                $("#send_status").html('<div class="error" style="color: #C00">Vul een geldig e-mailadres in om verder te gaan.</div>');
                $("#updates_subscription").focus();
          }
          else
          {
                var dataString = 'name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment + '&lang='+ lang;
                $.ajax({
                      type: "POST",
                      url: "../modules/site/process_contact_form.php",
                      data: dataString,
                      cache: false,
                      beforeSend: function()
                      {
                           $("#send_status").html('<div style="padding-left:100px;"><img src="loading.gif" alt="Sending...." align="absmiddle" title="Sending...."/> <font style="font-family:Verdana, Geneva, sans-serif; font-size:11px; color:black;">Please wait...</font></div><br clear="all">');
                      },
                      success: function(response)
                      {
                            $("#send_status").html($(response).fadeIn(2000));
    			$(".contL").fadeOut(1000);
                            $("#name").val('');
                            $("#email").val('');
    			$("#phone").val('');
    			$("#subject").val('');
    			$("#comment").val('');
                      }
                });
          }
    }
    Should I maybe add the &lang to the success: function ?

    Thank you in adance.
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  13. #13
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido. I finally got it working. I took a slightly different approach. I just added 3 hidden fields to the contact form with the value of those 3 variables that were not working:
    Code:
    		  <input type="hidden" id="salutation" value="<?php echo $salutation;?>" />
    		  <input type="hidden" id="message" value="<?php echo $message;?>" />
    		  <input type="hidden" id="signature" value="<?php echo $signature;?>" />
    That way they are picking up the right value depending on the includd translation file. I added those to the datastring and it is working like a charm. Thank you for all your patience and input
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)


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
  •