0
<script>
        $(document).ready(function(){
            $("#vE_rebtn").click(function{
            var reverifyEmail = '<?php echo $_SESSION["verifyEmIPv"]; ?>';
            $('#rE_rebtn').hide();
            $('#re_verifyEMAIL-notice').hide();
            $('#rE_resending').show();
            $.ajax({
                type: 'POST',
                url: 'data/system/auth.php',
                data: {verifyEMAIL: reverifyEmail},
                success: function (data) {
                    console.log(data);
                    $('#verifyEMAIL_notice').hide();
                    $('#re_verifyEMAIL_notice').show();
                }
            });
            });
        });
</script>
<ul id="verifyEMAIL_notice" class="success-notification"><li>Email Has been sent, hurry you have only 5 minutes to verify your IP and activate your account.</li></ul><br />
<ul id="re_verifyEMAIL_notice" style="display:none;" class="success-notification"><li>Email Has been sent, hurry you have only 5 minutes to verify your IP and activate your account.</li></ul><br /><h3><p>Keep in mind. Do not close this window, until you verify yourself.<br />If time limit exceed you can request for new code from this page.</p><br />
<div id="vE_rebtn">Click To Resend Now!</div></h3>   
<div id="vE_resending" style="display:none;">Resending.... Please Wait.</div></h3>

Above code not working

I want to send form data from this page, this page receive form data in $_POST variables and I now want if someone click on Resend Code then jquery run and send this form data to another php page and get result from that page.

epascarello
  • 204,599
  • 20
  • 195
  • 236
DevROYAL
  • 39
  • 1
  • 9
  • 1
    You can't echo php like that to javascript. Try creating an input on that page, echoing the $_SESSION value into that, then giving that input an id and hidden attribute and grabbing it with jquery that way. – clearshot66 Sep 15 '17 at 15:22
  • No I can't use input on that page, its against rules of scripts. I have to send form data via javascript without asking again from user – DevROYAL Sep 15 '17 at 15:24
  • What is the issue you're facing? – David R Sep 15 '17 at 15:25
  • This script can't run, I don't know why? I write to best of my knowledge but it is not working. – DevROYAL Sep 15 '17 at 15:26
  • please remember that you cannot use `` here in javascript. As @clearshot66 mentioned, please make an input field and assign value of your php variable to it and then call that input field in your javascript, that way will work. – Houy Narun Sep 15 '17 at 15:30
  • @HouyNarun (and clearshot66) Wrong, why would that not be possible? How is this any different from echo'ing it into an input? It's perfectly valid to echo a PHP variable into a JS variable. – rickdenhaan Sep 15 '17 at 15:32
  • So does the click event fire? Does the `reverifyEmail` have the value you expect? Did you try to add an error handler to the Ajax call and see if that is failing. – epascarello Sep 15 '17 at 15:35
  • @rickdenhaan, sorry, I tried and quite sure that it does not, instead it print `` php code directly, regardless of wrap it with single quote or double quote. . – Houy Narun Sep 15 '17 at 15:37
  • @HouyNarun Nonsense: https://3v4l.org/YfTg5 -- it's just basic echo'ing, PHP does not care where you do it as long as you're doing it in a `.php` file. Just like doing ``. – rickdenhaan Sep 15 '17 at 15:50
  • @rickdenhaan, oh god, I see. I used `echo` in *.js file and include that *.js file my *.php file, that will not work. however, I found another answer here https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript?rq=1, as they use `var data = ;`. Would you mind explaining the different? thanks. – Houy Narun Sep 15 '17 at 16:03
  • @HouyNarun using `json_encode()` ensures that you're echo'ing something Javascript will understand. Just plain echo'ing the PHP variable means you need to be 100% sure the value will be accepted. Like in this code, if the value from the session contains a `'` it will break. With json_encode you wouldn't need to worry about that. – rickdenhaan Sep 15 '17 at 16:07
  • @rickdenhaan, very much thanks, will give you another thumb up :) – Houy Narun Sep 15 '17 at 16:27

3 Answers3

1

You're missing parentheses at the onclick function declaration:

$("#vE_rebtn").click(function(){
// add this: ----------------^^

Your browser's console should be telling you that there's invalid syntax there with your current code.

Other than that, this code should work fine for sending the value of $_SESSION["verifyEmIPv"] to the server when the <div id="vE_rebtn"> is clicked.

rickdenhaan
  • 10,857
  • 28
  • 37
1

try this . i think it will be work

    <script>
    $(document).ready(function(){
        $("#vE_rebtn").on("click",function(){
        var reverifyEmail = $("#verifyEmIPv").val();
            alert(reverifyEmail);
        $('#rE_rebtn').hide();
        $('#re_verifyEMAIL-notice').hide();
        $('#rE_resending').show();
        $.ajax({
            type: 'POST',
            url: 'data/system/auth.php',
            data: {verifyEMAIL: reverifyEmail},
            success: function (data) {
                console.log(data);
                $('#verifyEMAIL_notice').hide();
                $('#re_verifyEMAIL_notice').show();
            }
        });
        });
    });
</script>
<input type="hidden" id="verifyEmIPv" value = "<?php echo 
$_SESSION["verifyEmIPv"]; ?>" />
 <ul id="verifyEMAIL_notice" class="success-notification"><li>Email 
Has been sent, hurry you have only 5 minutes to verify your IP and 
activate 
your account.</li></ul><br />
<ul id="re_verifyEMAIL_notice" style="display:none;" class="success-
notification"><li>Email Has been sent, hurry you have only 5 minutes 
to verify your IP and activate your account.</li></ul><br /><h3>
<p>Keep in 
mind. Do not close this window, until you verify yourself.<br />If 
time 
limit exceed you can request for new code from this page.</p><br />
<button id="vE_rebtn">Click To Resend Now!</button></h3>   
<div id="vE_resending" style="display:none;">Resending.... Please 
Wait.</div></h3>
0

If you just want to send the data and get result, you are doing too much here. Just create a jQuery function, put it in head tag or in any .js file you want and call it when you click on button any other element you want. Pass the ids or email ids you want. Validate before sending, then send the call. Once you receieve the data from web service or API just show the result in any span or alert box.

Why complicate?