views:

773

answers:

6
<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?

+4  A: 

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.

RaYell
A: 

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.

thephpdeveloper
A: 

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">
karim79
+1  A: 

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"

Gaurav Sharma
A: 
<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>
John
Hahah, nice naming.
brianreavis
I think i did a better job on this one: http://stackoverflow.com/questions/1410163/how-to-write-a-php-htaccess-script-to-redirect-form-variables-to-another-website/1410747#1410747 - but hey, i try ;)
John
+1  A: 

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.

BraedenP