0

I am working on a php file for school and we have to do some javascript verification of first name, last name and email. If everything is OK we can choose if we want to add the driver or cancel. if something is wrong you go back to the form. I did the verify script in a small page and got it to work there, but when I put it in my php file it will not work anymore. Here are some snippets of the code:

<script type = "text/javascript" >
function val_name()
{
    var namePattern = /^[A-Za-z]{3,25}$/;
    if( !namePattern.test(document.upload.fn.value))
            alert("Enter valid first name");

    if( !namePattern.test(document.upload.ln.value))
        alert("Enter valid last name");

    var x=document.forms["upload"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");

    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
    {
        alert("Not a valid e-mail address");
        return false;
    }
    else (confirm("Are you sure you want to add this driver?")== true)
    {
        window.location = 'add_driver.php';
    }
}
</script>

    <form id="upload" action="<?php echo $_SERVER['php_self'];?>" method="post" enctype="multipart/form-data" onsubmit="val_name()">

This is part of the html - the email part of it

<td>
email
</td>
<td>
<input type="text" id="email" name="email"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="add_driver" name="add_driver" value="add_driver" />
<input type="reset" value="clear form"/>
3
  • Try using the javascript console (F12 in chrome) to see if you're getting any errors. What exactly do you mean it isn't working? Commented Mar 20, 2013 at 2:08
  • What happens when you try this? Commented Mar 20, 2013 at 2:08
  • If the issue is that your add_driver.php isn't getting your form data, it might be because you are redirecting (window.location) instead of just allowing the form to submit. Commented Mar 20, 2013 at 2:12

1 Answer 1

1

It might just be a paste error, but the line

else (confirm("Are you sure you want to add this driver?")== true)

isn't likely doing what you expect. I believe you want an else if there.

As it is written, the confirm will pop up, but it doesn't actually control the flow of anything, so your next block of code

{
    window.location = 'add_driver.php';
}

will happen regardless.

Now, with you question as it is, I don't know if this is the error you're seeing. It is, however, at least one problem with your code.

Sign up to request clarification or add additional context in comments.

1 Comment

What I mean it isn't working is. I will skip a name and hit submit and it should bring up an alert box but nothing happens. No alert or anything.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.