0

How do I get values of HTML form value and pass it in PHP code inside javascript? I need to call a PHP function inside a javascript. Please kindly help me fix my code. Thank you very much.

<script>
function Send() {
var abc="<?php   echo mail_message.val();  //Email functionality here.. ?>";
document.write(abc);
}
</script>

<body>
<form>
<label class="email_label" for="mail_message">Message</label>
<textarea rows="7" cols="78" name="mail_message" id="mail_message">Enter your message here...</textarea>
<form>
</body>
Jemru
  • 2,091
  • 16
  • 39
  • 52

2 Answers2

3

PHP runs before your JavaScript gets loaded, so if you need to send data to PHP you need an AJAX request:

$.post('script.php', {
    value: $('#mail_message').val()
}, function() {
    // success?
});

Your script.php will then read $_POST['value'] and process it.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Hi Thanks for the advice, but can you please show me a more complete example? Actually the PHP function that I need to call inside the javascript is a PHP email functionality, so I don't want to move to another page. Thanks. – Jemru Apr 24 '14 at 07:29
  • What exactly do you mean by complete example? What are you confused about? – Ja͢ck Apr 24 '14 at 07:31
1

You can't use javascript val() in php syntex

instead use the following code to get the php value in javascript

<script>
function Send() {
   var abc=document.getElementById('mail_message').value;
   document.write(abc);
}
</script>
Keyur Mistry
  • 926
  • 6
  • 18
  • the PHP code that i want to put inside the Javascript function is PHP Email functionality. Thanks – Jemru Apr 24 '14 at 07:08
  • So you have to call external php function to this. So that your email process will be done. `` this will get the mail function in javascript; – Keyur Mistry Apr 24 '14 at 07:32