-1

I have a basic HTML form with one input text field, along with a submit button. Now, I want to use JavaScript to display the content entered by the user in the text field after form submission.

Here's the code of my form:

<form method = "POST">
   <input type = "text" name = "tweet1" />
   <br>
   <input type = "submit" onclick = "postResul()" />
</form>

On clicking the submit button, the postResul() function is called:

<script>

function postResul()
{
    var htmlString="<?php echo $_POST['tweet1']; ?>";
    alert(htmlString);
}

</script>

Now, both these code snippets are stored inside a PHP file. However, on submitting the form, the data entered in the input form field doesn't get displayed. I'm displaying the $_POST['tweet1'] variable in order to display the entry submitted by the user.

What seems to be wrong here? Am I using the $_POST variable in PHP the wrong way?

klenium
  • 2,468
  • 2
  • 24
  • 47
Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104
  • 8
    You have to realise that PHP and JS are very different languages—PHP works on the server side, and JS on the client side. When you call the `postResul()` function, you are attempting to echo a `$_POST` variable that is already processed n the server side—which is not your intention. You can either (1) simply alert the user's input value or (2) store the user input in a DB, for which you access their input using an AJAX call. – Terry Aug 23 '15 at 19:38
  • possible duplicate of [Call php function from javascript](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript) – Mark Amery Aug 23 '15 at 20:22

6 Answers6

2

If you want to display the input's value BEFORE sending it to your server:

document.querySelector("form").addEventListener("submit", function()
{
    var value = this.querySelector("input[name='tweet1']").value;
    alert(value);
    return false; //disable sending the data to the server
}, false);
    <form id="myForm" method="post" action="index.php">
       <input type="text" name="tweet1" />
       <br>
       <input type="submit" />
    </form>

If you want to display the input's value AFTER sending it to your server:

<form method="post" action="index.php">
   <input type="text" name="tweet1" />
   <br>
   <input type="submit" />
</form>
<?php echo htmlspecialchars($_POST["tweet1"]); ?>

These are different things. You can use $_POST only after you've sent some datas to the server. When you open yoursite.com/index.php in your browser, you make a HTTP GET request. In this case, $_POST will be an empty array, since it's a GET request, no data is sent to the server. When you submit the form, you make a HTTP POST request. Your PHP can access only that data you sent to the server. With Javascript, you work on the visitor's computer, not on the server. The only one way to send the data to the server without refresing the page, if you use AJAX, and make a new HTTP POST request, that'll run in the "background". But you do not need this if you just want to display the input's value, and you don't want to save it on your server. That can be done with Javascript, and without PHP.

The code you posted above would work like this:

  1. You make a HTTP GET request to yoursite.com/index.php.
  2. No data is sent to the server, $_POST will be empty.
  3. var htmlString="<?php echo $_POST['tweet1']; ?>"; In this line, you try to echo an non-existing member of $_POST, you might see an error if display_errors is not disabled.
  4. You click on the submit button.
  5. It has an onclick attribute, postResul (a Javascript function) is called. If you open the page's shource, you'll see this:

    function postResul()
    {
        var htmlString="";
        alert(htmlString);
    }
    
  6. After an empty popup is shown, and you press OK, the browser send the data to your server, and you'll able to acess the input's value via $_POST.

  7. If you press the submit button again, you'll see submited value (and not the input's actual value), because if you open the source code, you'll see this:

    function postResul()
    {
        var htmlString="entered data";
        alert(htmlString);
    }
    

But that isn't want you want, so see the examples above depending on what you want (save the data, or just display it in the browser).

klenium
  • 2,468
  • 2
  • 24
  • 47
1

This should work:

function postResul()
{
    var htmlString=document.getElementsByName("tweet1")[0].value;
    alert(htmlString);
}

But you should really read more on how client-side and server-side languages work.

John Bupit
  • 10,406
  • 8
  • 39
  • 75
1

You cannot use $_POST['tweet1'] to get the value when you are invoking a Javascript function. Basically client side and server side are totally different.

You can obtain the result using Javascript as:

function postResul()
{
    var htmlString= document.getElementsByName("tweet1")[0].value;
    alert(htmlString);
}

In HTML:

<form method = "POST">
   <input type = "text" name = "tweet1"/>
   <br>
   <input type = "submit" onclick = "postResul()" />

</form>

Note: The above function runs in client side and not in server side.

$_POST can be used to get values of the submitted form in a php page.

Cheers.

Roy M J
  • 6,926
  • 7
  • 51
  • 78
  • `$_POST can be used to get values of the submitted form in a php page.` I didn't quite understood what this meant. My original code was inside a php page itself. Can you elaborate a little more on this? – Manas Chaturvedi Aug 23 '15 at 19:47
  • @ManasChaturvedi: Javascript is something that runs on the client side. And the `onclick` function that you have written is meant to run on client side only(browser). When you submit form and you want to get the values in a php page, you do it via `$_POST['tweet1']`. Try echoing the `$_POST['tweet1'];` on top of your php page and you will know. – Roy M J Aug 23 '15 at 19:50
0

You have to make another file. Change your code to:

<form method="POST" action="another.php" >
   <input type = "text" name = "tweet1" />
   <br>
   <input type = "submit"  />
</form>

In file another.php you can show the variable then:

<?php echo htmlspecialchars($_POST['tweet1'], ENT_QUOTES, 'UTF-8'); ?>
SimZal
  • 291
  • 2
  • 3
0

You should use the form in a different way

<form method="POST">
   <input type = "text" name = "tweet1" />
   <br>
   <input type = "submit" />
</form>

test.php file

<?php
return json_encode([1, 2, 3]);

js

$('form').on('submit', function() {
   $.post('test.php', {}).done(function(response) {
      alert(response);
   })
})

Something like this. Hope it's useful.

Alejandro Garcia Anglada
  • 2,373
  • 1
  • 25
  • 41
0

if you are using jquery library you can do this

<form method="POST">
   <input type = "text" name = "tweet1" class="tweet" />
   <br>
   <input type = "submit" class="submit" />
</form>

$('.submit').click(function(e){
    alert($('.tweet').val());
    e.preventDefault();
});

jsfiddel working example http://jsfiddle.net/mdamia/j3w4af2w/2/

mdamia
  • 4,447
  • 1
  • 24
  • 23