2

I'm creating a script that first sends the code where the user gets a summary of the data, then he clicks a button to send the data to the webmaster's email.

I have this php data:

<?php
        $name = $_POST['name'];
        $email = $_POST['email'];
        ?>

Then I need ajax to get that data in a way it can read and submit to a php form:

<script type="text/javascript">
 $(document).ready(function() { //Quando documento estiver pronto
   $('#btnEnviarEmail').click(function() {
         var name = $('#name').val();
         var email = $('#email').val();

         var urlData = {
                     "name": name,
                     "email": email
                  };

         // Ajax 
         $.ajax({
             type: "POST",
             url: "sendmailivamdefinitivo.php", // endereço do phpmailer
             async: true,
             data: urlData,

             // informa Url
             success: function(data) { // sucesso 
                 $('#retornoHTML').html(data);

             },
             beforeSend: function() { // antes de enviar 
                 $('.loading').fadeIn('fast');
             },
             complete: function() { // completo 
                 $('.loading').fadeOut('fast');
             }

                });

           });
           });
     </script>

Should I use it this way to get the php strings?:

 var name = $('name').val();
 var email = $('email').val();
user3692451
  • 549
  • 1
  • 6
  • 20
  • You should be able to pass the content as you have defined it. But it's highly recommended that you do some sort of string cleaning first. Look for cleanQuery() function on the net ;) – MrTechie Apr 02 '15 at 05:03
  • So that method `var name = $('name').val();` is right, it just needs some cleaning now? – user3692451 Apr 02 '15 at 05:11
  • Yes, it should work. You load your page up in Chrome, and then right click and inspect element and then click on the network tab before you click anything. Then fill in the form and hit submit and look at the network tab. You should be able to see what's being passed or not passed. Then in the php script - make sure you clean the submission. – MrTechie Apr 02 '15 at 05:14
  • It's funny, cause it's not working :/ – user3692451 Apr 02 '15 at 05:17
  • Have you looked in Chrome Dev Tools to see what's happening with it? – MrTechie Apr 02 '15 at 05:18
  • You should use jquery's serialize() instead and then do what ever you want with input data in your php function. – Vineet Apr 02 '15 at 05:19
  • I looked into the console debugger and the network tab, there is nothing related to the form. – user3692451 Apr 02 '15 at 05:26
  • IF anyone wants to text. Dont mind about the language, just filling all fields and hit the button, then try the next button: http://sitesdemo.mghospedagem.com/ivam-entregas/3/33209.html – user3692451 Apr 02 '15 at 05:43
  • Possible duplicate: http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – callback Apr 02 '15 at 05:48

1 Answers1

0

No.

Use this:

var name = "<?php echo $name; ?>";
var email = "<?php echo $email; ?>";

This will echo the PHP vars in output (means in JS code). so the value of $name and $email will be store in JS variables (as it's encode by "").

Just remember that the PHP variables should be defined before the JavaScript tag(that is obvious).


Edit: As OP asked in comment, My understanding to question is:

There is two PAGE, on first page user submit some values(probably a code), PHP received those values and prepare summary of data in variables. Now on second page, those variables are displayed to user(for review/confiermation), then user will send data to webmaster by AJAX. Problem is how to get PHP var's value in AJAX var.

1st Page submission -> PHP (have data in var) -> AJAX (send PHP var's data to webmaster).

Sample source: (second page)

<?php $name=$_POST["name"]; //from previous page submission...
      $email=$_POST["email"];// ...(previous page can be same PHP page)
      $someOtherVar="summary"; ?>

<SCRIPT type="javascript/text">
   var name = "<?php echo $name; ?>"; //declaring JS vars having value of PHP vars
   var email = "<?php echo $email; ?>";

   //JS code to make AJAX request to send these JS vars (same as OP's)
</SCRIPT>

<body>
   displaying summary here by php
   <?php 
      echo $name;
      echo $email;
     //..... other summary data if there
   ?><br />
   <input type="button" id="btnEnviarEmail" value="SEND to Webmaster/Confirm" />
</body>
Adarsh Rajput
  • 1,246
  • 14
  • 24
  • Can I ask? What is your source? I've never seen it that way. – user3692451 Apr 02 '15 at 05:44
  • @user3692451: As per as I understand your question, you have some PHP variables(string values:-which you got from previous user submission), and now in current page you want to resubmit strings by AJAX(so that user can review and resend/confirm). | **There is 2 ways:** echo PHP-vars in INPUT tag and get value in JS by functions **OR** directly echo in JS var(as I did.). | **If my understanding to question is wrong(see my edited answer) then clear me, I'll try again.** – Adarsh Rajput Apr 02 '15 at 06:19
  • @user3692451: Learning/Understanding is not getting explanation first(at least not always), give solution a try-if it will work then your mind will be open to receive/find explanation as mind seen solution working. This is psychological things. **|** _if solution doesn't work,_ THEN I m sure, I'm wrong in getting your question, coz this code works for me always. **Please correct me then..** – Adarsh Rajput Apr 02 '15 at 06:35
  • Man you rock! I tried it the way you specified and it worked like a charm. I just have to add something, I had a checkbox in code too, see this is how I extracted the php checkbox wth ajax: `var checkbox = $(':checked').val() ? 'Yes' : 'No';` – user3692451 Apr 02 '15 at 06:52
  • @user3692451: I m happy that it works. | I have a doubt, what's the value of PHP var `$checkbox`? coz in jQuery selector `$(html-tag-name OR id-of-html-tag-with-# OR class-of-html-tag-with-leading-dot')`, whatever you have echo by PHP should be a valid [CSS selector](http://www.w3schools.com/cssref/css_selectors.asp) – Adarsh Rajput Apr 02 '15 at 07:26
  • A ajax form sends `var idaevolta = $('#cbIdaVolta:checked').val() ? 'Sim' : 'Não';` which is converted with a php code `$idaevolta = $_POST['idaevolta'];` and then the `$idaevolta` is used in ajax, with the way you thaught me. – user3692451 Apr 03 '15 at 02:49
  • I converst from html to javascript to php to javascript to php. – user3692451 Apr 03 '15 at 02:50
  • @user3692451: **1st comment**, View source in browser you'll have the value in place of PHP-code [ `$( 'some_value:checked' ).val() ? 'Yes' : 'No';` ] ----------------------------------------------- **2nd/3rd comment**, That is correct. if checkbox(having id=cbIdaVolta) is checked then JS took value from HTML by css-selector(`#cbIdaVolta:checked`)[**HTML->JS**]; then sends to PHP. PHP Retrieve it by POST-DATA[**JS->PHP**] and echoes in **JS** so that it can be send again by AJAX[**PHP->JS**]. And at last after user click it send to PHP(sendmailivamdefinitivo.php / webmaster)[**JS->PHP**]. – Adarsh Rajput Apr 04 '15 at 05:37