-3

Is this possible? Or do I really need to AJAX JS first?

<?php
echo'< script type="text/javascript">var eadd=prompt("Please enter your email address");< /script>';
$eadd = $_POST['eadd']; ?>

and how can I do that with AJAX?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Ken
  • 27
  • 1
  • 1
  • 5
  • It is not possible, you have to do an Ajax request to be able to talk from javascript to php. – Yoann Oct 06 '14 at 04:22
  • Can you tell me how? Thanks – Ken Oct 06 '14 at 04:25
  • With jQuery `$.post('/url', data, callback);` http://api.jquery.com/jquery.post/ – Yoann Oct 06 '14 at 04:27
  • 3
    That is not how the internet works. You should learn PHP and Javascript before going into AJAX, imo. – Sverri M. Olsen Oct 06 '14 at 04:27
  • @SverriM.Olsen, well I'm quite familiar with PHP and Javascript, but not in AJAX. It's just that when I try to run my PHP it doesn't read the variable I set for the script. – Ken Oct 06 '14 at 04:30
  • 1
    @Ken The code in your question suggests otherwise. If you are, indeed, familiar with both PHP and Javascript then you need to familiarise yourself with what they can and cannot do. In order for the browser to speak to the server, without going to another page, you have to use something like AJAX (or some newer technologies). – Sverri M. Olsen Oct 06 '14 at 04:43
  • @SverriM.Olsen I just said I'm 'quite' familiar with it, but not totally familiar, I'm still in highschool, and I just need it for my school presentation. Thanks for the advice though. :) – Ken Oct 06 '14 at 04:48

3 Answers3

4

Its not possible. You should use ajax. jQuery was used in the following example:

<script>
var eadd=prompt("Please enter your email address");
$.ajax(
{
    type: "POST",
    url: "/sample.php",
    data: eadd,
    success: function(data, textStatus, jqXHR)
    {
        console.log(data);
    }
});
</script>

in php file

<?php
echo $_POST['data'];
?>
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
divakar
  • 1,379
  • 6
  • 15
  • 31
2

Ajax (using jQuery)

<script type="text/javascript">
$(document).ready(function(){

var email_value = prompt('Please enter your email address');
if(email_value !== null){
    //post the field with ajax
    $.ajax({
        url: 'email.php',
        type: 'POST',
        dataType: 'text',
        data: {data : email_value},
        success: function(response){ 
         //do anything with the response
          console.log(response);
        }       
    }); 
}

});
</script>

PHP

echo 'response = '.$_POST['data'];

Output:(console)

response = email@test.com

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
meda
  • 45,103
  • 14
  • 92
  • 122
2

Is not possible directly. Because PHP is executed first on the server side and then the javascript is loaded in the client side (generally a browser)

However there are some options with or without ajax. See the next.

With ajax. There are a lot of variations, but basically you can do this:

//using jquery or zepto
var foo = prompt('something');
$.ajax({
    type: 'GET', //or POST
    url: 'the_php_script.php?foo=' + foo
    success: function(response){
        console.log(response);
    }
});

and the php file

<?php
    echo ($_GET['foo']? $_GET['foo'] : 'none');
?>

Witout ajax: If you want to pass a value from javascript to PHP without ajax, an example would be this (although there may be another way to do):

//javascript, using jquery or zepto
var foo = prompt('something');
//save the foo value in a input form
$('form#the-form input[name=foo]').val(foo);

the html code:

<!-- send the value from a html form-->
<form id="the-form">
    <input type="text" name="foo" />
    <input type="submit"/>
</form>

and the php:

<?php
    //print the foo value
    echo ($_POST['foo'] ? $_POST['foo'] : 'none');
?>
rogelio
  • 1,541
  • 15
  • 19