Link to home
Start Free TrialLog in
Avatar of JJ123
JJ123

asked on

Allowing Users to Use the Carriage Return to submit a form

I have a form that I need to allow users to scan in data into a field. The barcode that they are scanning has a carriage return at the end. I need to allow that carriage return to submit the form rather than having them hit enter. I tried to use some JavaScript to do this but I am having no luck. Here is my code:

<?php
include 'include/connect.include';
$page="New Orders";

include 'include/header.include';

?>
<SCRIPT LANGUAGE="JavaScript">
<!--

function checkEnter(e)
{ //e is event object passed from function invocation
      var characterCode //literal character code will be stored in this variable

      if(e && e.which)
      { //if which property of event object is supported (NN4)
            e = e
            characterCode = e.which //character code is contained in NN4's which property
      }
      else
      {
            e = event
            characterCode = e.keyCode //character code is contained in IE's keyCode property
      }
      //alert(e);
      if(characterCode == 13)
      { //if generated character code is equal to ascii 13 (if enter key)
            document.forms[0].submit() //submit the form
            return false
      }
      else
      {
            return true
      }

}

-->
</script>

<?php

      if(isset($_GET['error']))
      {
            $error = $_GET['error'];
            print "<font color='red'><b>$error</font></b><br>";
            
      }
if(!isset($_REQUEST['new']) && !isset($_GET['pk']))
{
      ?>
      <!-- <form action='new_orders.php' method='post'> -->
      <table>
      <tr>
      <td>Supervisor:</td><td><input type='text' name='supervisor'></td>
      <td>Picker:</td><td><input type='text' name='picker'></td>
      <td><input type='submit' name='new' class='bn' value='Submit'></td>
      </tr>
      </table>
      </form>
      <?php
}
else
{      
      
      if(isset($_REQUEST['supervisor']))
      {$supervisor = $_REQUEST['supervisor'];}
      if(isset($_GET['sv']))
      {$supervisor=$_REQUEST['sv'];}

      if(isset($_REQUEST['picker']))
      {$picker = $_REQUEST['picker'];}
      if(isset($_GET['pk']))
      {$picker = $_GET['pk'];}

      $query = "SELECT * FROM orders WHERE picker = '$picker' AND qa = '0' AND inv_issue='0'
      AND packed='0' AND xship='0' AND ship_confirm='0' AND xship_issue='0'";
      $result = mysql_query($query) or die(mysql_error());
      $line = mysql_fetch_array($result);
      if(mysql_numrows($result) > 0)
      {
            $supervisor = $line['supervisor'];
            $picker = $line['picker'];
            print "<font color='red'><b>Attention:This picker already has an active pick list.</font></b><br>";
      }
      if(isset($_GET['error']))
      {
            $error = $_GET['error'];
            print "<font color='red'><b>$error</font></b><br>";
            
      }
      if($picker == '' || $supervisor == '')
      {
            print "<font color='red'><b>Supervisor and picker names are required.</font></b><br>";
            exit();
      }
      ?>
      
      <table>
      <tr>
      <td><big>Supervisor:</b></td><td><big><?php echo $supervisor; ?></td></tr>
      <tr><td><big>Picker: </b></td><td><big><?php echo $picker ?></td>
      </tr></table>

      <br><b>Enter a new order to this pick list below:<br></b>
      <form action='results.php' method='post' name='order'>
      <table>
      <tr><td><center><b>Movement Order</b></td><td><b>Hot</b></td>
      </tr>
      <tr>
      <td><input type='text' name='order_num' onKeyPress="checkEnter(event)"> </td>
      <td><input type='checkbox' name='hot'></td>
      
      <input type='hidden' name='supervisor' value='<?php echo $supervisor; ?>'>
      <input type='hidden' name='picker' value='<?php echo $picker; ?>'>
      <input type='hidden' name='list_id' value='<?php echo $line['pick_list_id']; ?>'>
      <td colspan='10'><center><input type='submit' class='bn' value='Save' name='new_pick'></td></tr>
      </table>
      </form>
      <?php

      
      $qpick = "SELECT * FROM orders WHERE picker = '$picker' AND qa = '0' AND inv_issue='0'
      AND packed='0' AND xship='0' AND ship_confirm='0' AND xship_issue='0'";

      $rpick = mysql_query($qpick) or die(mysql_error());
      if(mysql_numrows($rpick) != '0')
      {
            ?>
            <br><b>Below is the current/active pick list for this picker:<br>
            <table>
            <tr><td><center><b>Movement Order</b></td><td><b>Hot</b></td>
            </tr>
            <?php

            for($i=0;$i<mysql_numrows($rpick);$i++)
            {
                  $pick_list = mysql_fetch_array($rpick);
                  ?>
                  <form action='results.php' method='post'>
            
                  <tr>
             <td><input type='text' name='order_num' value='<?php echo $pick_list['order_num']; ?>'></td>
                  <?php
                  if($pick_list['hot'] == '1')
                  {print"<td><input type='checkbox' name='hot' checked='checked'></td>";}
                  else
                  {print"<td><input type='checkbox' name='hot'></td>";}
                  ?>
                  
                  <input type='hidden' name='org_order_num' value='<?php echo $pick_list['order_num'] ?>'>      
                  <input type='hidden' name='list_id' value='<?php echo $pick_list['pick_list_id'] ?>'>      
                  <input type='hidden' name='picker' value='<?php echo $pick_list['picker'] ?>'>
      
                  <td colspan='10'><center><input type='submit' class='bn' value='Edit' name='change'></td></tr>
                  
                  </form>
                  <?php
            }
            print "</table>";

            ?>
      <br>To transfer this pick list to another picker, scan the name below and click transfer.<br>
      <form action='results.php' method='post'>
      <input type='text' name='new_picker'>      
      <input type='hidden' name='list_id' value='<?php echo $pick_list['pick_list_id'] ?>'>      
      <input type='submit' name='transfer' value='Transfer'>
      </form>
      <?php
      }      
}
include 'include/footer.include';

Any help would be appreciated.

Thank you!
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

JJ123,

You may have a hard time finding an event to use in this case.  Try using the onchange event as that will probably be the best bet.  The usual events depend on a key or mouse action.  You didn't specify exactly which field this would happen in but I will assume it is the order_num input box.

     <td><input type='text' name='order_num' onchange="checkEnter(event)"> </td>

If it doesn't work and it is a different field then please let me know which.  It may also help to get the relevant HTML source instead of the server script if you have to post other code.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of JJ123
JJ123

ASKER

You are right it is the order_num field that I need this event on. In the code above I was trying the onkeypress event. Would onchange be better? Well I've tried the onchange even as well and it is still not working.
From the information in the question it seems that a key isn't really pressed in that field.  The information is "sent" with the barcode reader.  It doesn't seem like a key event would work so that is why I suggested onchange.  It is fired when a value is changed which should be happening as the info is sent to the field.  Did you get a javascript error after changing the event to onchange?  An error would give us hope that the event may still work.

Now that I think about it the function will need to change too.  That function test to see what key is pressed.  I have to take off right now but I will work on making one that will work in this case.  Test to see if the onchange event is getting fired (and how often) by changing the tag to the one below.  When you "scan" the value does the alert appear?  More than once?

     <td><input type='text' name='order_num' onchange="alert(this.value)"> </td>

bol
The event onchange won't work either.  The field is still in focus.

From what I have read the onkeyup event may work.  I don't have a way to test this though.  Please try the line below in the input tag to see if that event is fired after or as the reader is used.  If so, is the length of the data from the barcode reader always the same or do you just have the carriage return to use to know it is done?

onkeyup="alert('keyup');"

bol
Avatar of JJ123

ASKER

The length of the barcode can be different. So it's the carriage return that determines the end of the line. Unfortunately I don't have a scanner available myself until tomorrow. So I will get this code in there and let you know what happens.

So taking a look at the JavaScript function, do you think that it will work?
We would probably have to change the function.  First let's test to see if the onkeyup event is fired just with the scanner and info it inserts.   Also when that seems to happen and if it is only once.  The most important is if it happens.  Thanks for the details on what is sent.  If the content was always the same length then it would be easier to test for it.

Let me know the results of the test when you can.  If successful then I will work on a function for it.

bol
Avatar of JJ123

ASKER

Sorry for the delay bol.

I finally got some testing done. When the barcode is scanned in, the 'keyup' pop up box does appear. So that's good. But it doesn't allow the whole value of the barcode into the text field. It just allows the first character into the field and then the pop up appears.
Let's try one more test.  Instead of that alert use this ...

onkeyup="if (this.value.match(/[\w]+\n/) alert('true'); return true;"

Let me know if all of the characters are put in and if you get a "true" alert.

bol
Avatar of JJ123

ASKER

No alert popped up with that event. I even tried entering information manually and hitting the enter key and still no alert.

But I was able to scan the whole barcode in.
What type of data is before the carriage return?  Any spaces or characters besides a normal word character or digit?  Also, there are different ways for the "carriage return" to appear.  You could try changing \n to \r\n in the script I provided.

Did we already try using your current function with the onkeyup event instead of the onkeypress event you originally had?

bol
Avatar of JJ123

ASKER

No the data entered doesn't have spaces or special characters so that should be fine. I just tried putting the \r\n into your script and it still did not display the alert.

No I never tried using onkeyup with the function that I already had. I will try that now.
Avatar of JJ123

ASKER

I just noticed that there was a Java error in the following line:

<td><input type='text' name='order_num' onkeyup="if (this.value.match(/[\w]+\r\n/) alert('true'); return true;"> </td>

That is probably why it's not working. The error says expected ')'. So I tried:

<td><input type='text' name='order_num' onkeyup="if (this.value.match(/[\w]+\r\n/) alert('true'); return true;)"> </td>

And I still get the same error and I don't get the popup.
 
Sorry about the typo.  The line should've been ...

<td><input type='text' name='order_num' onkeyup="if (this.value.match(/[\w]+\r\n/)) alert('true'); return true;"> </td>

The missing closing parenthesis needed to be before the alert.  Give that a try and hopefully you get the alert.  That error was probably in the first try to that just used \n.  You could try that again too.  You can also test your original script and use the onkeyup event instead.

Sorry for the typo. :)

bol
Avatar of JJ123

ASKER

Well it actually looks to be working now! So I will have a more thourough test of it on Wednesday.

I will also test my original script too - I will let you know what happens.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JJ123

ASKER

I am so sorry bol - I meant to close this and give you the points. Thank you for all your help - again sorry for the delay.

Thanks,
JJ123
Your welcome!  Thanks for taking the time to close it.  I'm glad that I could help.  Thank you for the grade, the points and the fun question.

bol