开发者

POST value not show in javascripts

开发者 https://www.devze.com 2023-02-08 21:19 出处:网络
from my this code <script type=\"text/javascript\"> function getValue() { console.log(\"<?php echo $_POST[\'input_value\'] ; ?>\") ;

from my this code

<script type="text/javascript">
function getValue()
{
    console.log("<?php echo $_POST['input_value'] ; ?>") ;
    return false ;
}
</script>

<form id="request_form" action="" name="request_form" method="POST" >
    <input id="input_value" type="text" size="45" name="input_value"></br>
    <button class="button" id="btForm" onclick="return getValue()" >submit</button>
</form>

I expect it will print some value that I开发者_如何学Go put but it show nothing. What am I wrong?


Your form isn't actually submitting to a php page. It's just calling getValue, which won't have anything in it yet.

There are two halves to what you are trying to do, and your mistake is that you have mixed them together. The first is the server side, with the PHP. When you submit a form, you should use:

<input type=submit value="Submit" />

This refreshes the page (because the form action is blank), which, using PHP, can read your POST value and print it back out along with the rest of the page. Once the page is finished loading, PHP is no longer involved and cannot affect the behavior.

As you have it right now, when you load your page it looks like this:

<script type="text/javascript">
function getValue()
{
    console.log("") ;
    return false ;
}
</script>

Since the POST value wasn't there the first time the page loaded, the javascript sent to the browser looks like that - there is no value to print. Clicking the button will execute getValue() as it appears here.


PHP is server side, and is done before the page is loaded in the browser. When first loaded there is no $_POST at the time, so the log is empty.

When submitting the form, you are calling the console.log function which has no text in it, and then returning FALSE, so the form never actually gets submitted.

You need to either set the action attribute in the form tag to point to a PHP page to process the $_POST data, or you need to use AJAX to post the form.


function getValue() {
     console.log(document.request_form.input_value.value) ;
     return false ; 
} 

could have a sense, at the moment whe you execute getValue () in JS (client-side) - PHP (server-side) is not involved so no way to echo $_POST....


You can't write out actual server-side code from javascript, and expect it to execute client-side.

0

精彩评论

暂无评论...
验证码 换一张
取 消