0

Would appreciate any help from anyone... the below PHP contains a list of items from a txt file, next to each item is a text box for the user to enter the qty, by default set to 1. I'm trying to clear the box when the user clicks on a text box, any ideas?

<input type='text' id='qty' name='$partno' size='1' value='0'>

So far I've tried using JavaScript but had no luck.

        <script>
        function validateName()
        {
            var x=document.forms["purchase"]["visitor"].value;
            if (x==null || x=="")
            {
                alert("Visitor name must be entered");
                return false;
            }
        }

        function name()
        {  
            document.getElementById('qty').value = "";
        }
    </script>
</head>

<body>

    <h1>Items Available</h1>

    <form id="purchase" name="purchase" action="confirm.php" method="post" onsubmit="return validateName()">
        <table>

            <h3>Visitor Name: <input type='text' name='visitor'></h3>
            <tr><th></th><th></th><th></th><th>Price</th><th>QTY</th></tr>

            <?php
            if (!($data = file('items.txt'))) {
                echo 'ERROR: Failed to open file! </body></html>';
                exit;
            }
            foreach ($data as $thedata) {

                list($partno, $name, $description, $price, $image) = explode('|', $thedata);
                echo "<tr><td><img src='$image' width='60' height='60' alt='image'></td><td><h3>$name</h3></td><td>&nbsp;&nbsp;&nbsp;&nbsp;$description</td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;<b>&pound;$price</b></td><td><input type='text' id='qty' name='$partno' size='1' value='0'></td></tr>";
                //<input type='text' size='1' name='partno_qty' value='1'</td>
            }
            ?> 

        </table>

        <input type="submit" value="Purchase">
        <input type="reset" value="Clear">

    </form>
2
  • The PHP for this is rather irrelevant to the question; I think it'd be helpful if you just posted the resulting HTML without the PHP for better readability. Commented Mar 27, 2013 at 19:39
  • 1
    In addition, you setting identical id attributes in a for loop, resulting in duplicate ids on the same page. An ID can only occur once within an HTML document; it needs to be unique. Commented Mar 27, 2013 at 19:40

2 Answers 2

1

Add an onclick or onfocus handler to your "input name=qty" tag, or look at using a "placeholder" attribute, which may not work in some browsers you have to support:

foreach ($data as $thedata)
{
    list($partno, $name, $description, $price, $image) = explode('|', $thedata);
                echo '<tr><td><img src="' . $image . '" width="60" height="60" alt="image"></td>'
                . '<td><h3>' . $name . '</h3></td><td>&nbsp;&nbsp;&nbsp;&nbsp;' . $description . '</td>'
                . '<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>&pound;' . $price . '</b></td>'
                . '<td><input type="text" id="part-' . $partno . '" '
                . 'name="' . $partno . '" size="1" '
                . 'value="0" onfocus="this.value=\'\';" placeholder="0"></td>'
                . '</tr>';
}

The "onclick/onfocus" approach will clear the box every time it's clicked/entered. If you wish to use your "name" function, the call changes to this:

onfocus="name( this );"

And "name" changes to:

function name( obj )
{  
    obj.value = "";
}
Sign up to request clarification or add additional context in comments.

5 Comments

Might be better to use onFocus so that it works if the user tabs to the field
I can't since the name is a variable.
Sure you can. If you must call a function to perform the action, you can always pass "this" to it, which is a reference to the object receiving the event.
I problem is the txt box is within PHP so the onfocus syntax doesn't comply.
The "name" function you defined is not in PHP, it's in JavaScript. Passing "this" to an onfocus event handler will work, and it's completely separate from PHP. I editied my answer above to create unique IDs for your text boxes, too.
0

you can use HTML5 placeholder and required attributes:

<form>
<input type="text" placeholder="Name" required />
<input type="submit" value="submit" />
</form>

DEMO

Notes:
The placeholder attribute is supported in Internet Explorer 10, Firefox, Opera, Chrome, and Safari. The required tag is supported in Internet Explorer 10, Firefox, Opera, and Chrome. It's not supported in Safari

or you can do it in JS like this:

<input type="text" onfocus="inputFocus(this, 'initial text')" onblur="inputBlur(this, 'initial text')" value="initial text" />

'initial text' is the default value of the text input when page loads e.g.('Your Name').

function inputFocus(elm, placeholder){
    if (elm.value == placeholder){ elm.value = "" }

}

function inputBlur(elm, placeholder){
    if (elm.value == "" || elm.value == placeholder){
        elm.style.border = "1px solid red";
        elm.value = placeholder;
        alert("Visitor name must be entered");
    }else{
        elm.style.border = "1px solid green";
    }
}

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.