SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statement in onclick event

    It always says "nothing in text box"

    PHP Code:
    <input type="text" class="formSmallTextbox" name="a_number" size="4" value="<?="$a_number"?>">
    PHP Code:
    <input type="submit" name="submit" class="formTextbox" value="Submit" onClick="<?php
        
    if ($a_number >= '1'){
        echo 
    "return confirm('something in text box')\">";}                else{
        echo 
    "return confirm('nothing in text box')\">";}
        
    ?>
    Last edited by matthewst; Jun 11, 2007 at 09:06.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, then that means $qty_oak is always < 1 and you should find out why that is. I also suspect you may be confusing client and server side scripting. There's no point in using a javascript confirm if the logic behind it is handled before the page has even been presented to the user.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It just occurred to me that when the page loads the text box a_number is always empty thats why it echos "nothing in text box". How do I get php to recognize that something was put in the text box after the page was loaded?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, you are confusing client-side and server-side scripting. This:
    PHP Code:
    <input type="submit" name="submit" class="formTextbox" value="Submit" onClick="<?php

        
    if ($qty_oak >= '1'){

        echo 
    "return confirm('something in text box')\">";}                else{

        echo 
    "return confirm('nothing in text box')\">";}

        
    ?>
    will output the following static HTML:
    HTML Code:
    <input type="submit" name="submit" class="formTextbox" value="Submit" onClick="return confirm('something in text box')">
    if your PHP variable $qty_oak is >= 1. Otherwise it will return a confirm prompt with the other text, regardless of what is in the text input.

    PHP has no control over the page once it has loaded. As soon as PHP sends the page to the user, that's its job done and anything dynamic must be solely handled by javascript (unless you're using AJAX). So the answer to How do I get php to recognize that something was put in the text box after the page was loaded? is "you can't". That's javascript's job. I think what you want is to do this:

    HTML Code:
    <input type="text" class="formSmallTextbox" name="a_number" size="4" value="">
    
    <input type="submit" name="submit" class="formTextbox" value="Submit" onClick="if (document.getElementsByName('a_number')[0].value == '') return confirm('nothing in text box'); else return confirm('something in textbox');">
    That, however, is horribly messy and you should consider using unobtrusive javascript.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks raffles that works

    i'll look into unobtrusive javascript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •