-5

I am new to AJAX Request, and my project need help.

JQuery / AJAX part :

<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'POST',
            url: 'ajax_save.php',]
            success: function(msg) {
                alert(msg);
            }
            error: function(msg) {
                alert('error');
            }
        });
    });
});
</script>

html part :

<input type="text" id="txt" name="text" />
<span id="txt">Result Maybe Here</span>

<button>Get External Content</button>

ajax_save.php

echo base64_encode( $_GET["text"] );

This don't really perform well, and just silent with out any trace.

Example of performing well :

<input type="text" id="txt" name="text" /> >>> agnes (user type)
<span id="txt">Result Maybe Here</span> >>> YWduZXM= (result)
<button>Get External Content</button>

1 Answers1

0

About Ajax

To perform AJAX request on browser(client side), you need JavaScript. Maybe you can take a look at jQuery.ajax. $.get('process.php?text=something');

To perform AJAX request in PHP(server side), use file_get_contents('process.php?text=something');

To make your code work, you should perform a GET request instead of a POST one:

<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'GET',
            url: 'ajax_save.php?text=' + $('#text').val(),
            success: function(msg) {
                $('#span').text(msg)
            }
            error: function(msg) {
                alert('error');
            }
        });
    });
});
</script>

And every id in your HTML element should be unique, so they shouldn't both be id="txt".

About Base64

BTW, if you're just going to perform Base64 encoding, why not do it in JavaScript?

$(function(){
    $('#text').on('input', function(){
        $('#span').text(btoa($(this).val()));
    });
});
<input type="text" id="text" name="text" />
<span id="span">Here the result!</span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
NoobTW
  • 2,466
  • 2
  • 24
  • 42