admin管理员组

文章数量:1295742

<input type="button" name="continue" id="continue" value="Continue" onclick="<? 
            if($_POST['rules']==null) {
                echo "hello();";
                }
            elseif($_POST['rules']!=null) {
                echo "myRedirect();";
            }   

                 ?>" > 

i have a form with a textarea. when the user hits the button when the textarea is null it should do the function hello() and if the textarea is not empty it should do the function myRedirect(). the function hello() and myRedirect() generates different dialog boxes. but it doesn't seem to work. what's wrong with this code?

<input type="button" name="continue" id="continue" value="Continue" onclick="<? 
            if($_POST['rules']==null) {
                echo "hello();";
                }
            elseif($_POST['rules']!=null) {
                echo "myRedirect();";
            }   

                 ?>" > 

i have a form with a textarea. when the user hits the button when the textarea is null it should do the function hello() and if the textarea is not empty it should do the function myRedirect(). the function hello() and myRedirect() generates different dialog boxes. but it doesn't seem to work. what's wrong with this code?

Share asked Sep 12, 2009 at 6:35 noobnoob 4,78710 gold badges34 silver badges32 bronze badges 1
  • 1 Why cant u just edit your old questions? stackoverflow./questions/1414422/help-in-php-button – Shoban Commented Sep 12, 2009 at 6:42
Add a ment  | 

6 Answers 6

Reset to default 4

It's not possible like that. JavaScript (onlick) executes in client browser while PHP executes on your server. IF you really need to execute PHP code you should use AJAX to send another request for your server to do PHP part.

If you're simply checking the value of the box, you just need Javascript... This should work fine:

<script type="text/javascript">
function check(obj_id){
    var result = document.getElementById(obj_id).value;
    if(result == ""){
        is_null();
    }else{
        is_not_null();
    }
}

function is_null(){
    alert("null");
}

function is_not_null(){
    alert("not null");
}
</script>
<input type="text" id="test" />
<button onclick="check('test')">Test</button>

Pass the id of the textbox you want to check into the check() function, and it finds whether anything has been entered into the box. If it has, it calls the function "is_null()" and "is_not_null()" if there is something in the textbox.

I think it will not work at all because you are trying to call a PHP code from javascript. In other words PHP is a server side scripting language and you are trying to execute a server side code that needs to be submitted in order to get executed.

To achieve this functionality you can simply write this code

 if($_POST['rules']==null)
 {
  echo "hello();";
 }
 elseif($_POST['rules']!=null)
  {
  echo "myRedirect();";
 }

at the top of your PHP page and you will get what you want. or make the input type as "submit"

You probably need a postback to the server to execute whatever you want to run in the onclick event.

Another work around is to use AJAX.

Are you mixing up client side and server side? Is this what you're after?

<?php
if($_POST['rules'] == null) {
    echo "hello";
} else {
    myRedirect();
}
?>

<input type="submit" name="continue" id="continue" value="Continue">
<script>
function hello(){}
function myRedirect(){}
function ifnullz(el){
  if($(el)!=0){
    hello();
  }else{
    myRedirect();
  }
}
function $(el){
   document.getElementByID(el).length; //.value?? idk.. something like that.
}
</script>

<input onlcick="ifnullz('lolz');">
<textarea id="lolz"></ta>

本文标签: javascriptputting php codes on onclick event of a buttonStack Overflow