Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    putting a line break in PHP

    I wrote a javascript validation and in the message I added \n. But when I have this line break, I am getting a javascript error "Unterminated string constant". This happens only when I save a page as .php. But when I save and test as .html everything works fine. Why is this happening only when the page is a .php page?

    Here is a portion of the javascript

    <script language="javascript">
    var submitted=0;


    function checkForm(TheForm)
    {
    var msg = "Please fill in all the fields:\n";
    var errorField = null;

    if (submitted==0)
    {
    // name, email, company, address etc
    if (TheForm.name.value.length == 0) {
    msg += " * Please enter your first name.\n";
    if (errorField == null) {
    errorField = TheForm.name;
    }

    }
    if (errorField != null) {
    alert(msg);
    errorField.focus();
    return false;
    }

    submitted=1;
    }
    else {
    alert('The form has already been successfully submitted.');
    return false;
    }
    }
    </script>

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is your warning a javascript or PHP error?


    Any HTML or Javascript should not effect the PHP processing.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is a javascript error. But if I take that same javascript funciton and insert into an html page then it works

  • #4
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Give an example of the PHP & javascript code and the error message

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Following is the code. The error I am getting is "Unterminated string constant" at line 13. THe line 13 is the first msg with \n

    <?

    $section="contact us";
    $title="contact us";
    $head = <<< HEAD

    <script language="javascript">
    var submitted=0;


    function checkForm(TheForm)
    {
    var msg = "Please fill in all the fields:\n";
    var errorField = null;

    if (submitted==0)
    {
    // name, email, company, address etc
    if (TheForm.name.value.length == 0) {
    msg += " * Please enter your first name.\n";
    if (errorField == null) {
    errorField = TheForm.name;
    }

    }
    if (TheForm.email.value.length == 0) {
    msg += " * Please enter your email.\n";
    if (errorField == null) {
    errorField = TheForm.email;
    }

    }
    if (TheForm.company.value.length == 0) {
    msg += " * Please enter your company.\n";
    if (errorField == null) {
    errorField = TheForm.company;
    }

    }
    if (TheForm.address.value.length == 0) {
    msg += " * Please enter your address.\n";
    if (errorField == null) {
    errorField = TheForm.address;
    }

    }
    if (TheForm.zip.value.length == 0) {
    msg += " * Please enter your state/zip.\n";
    if (errorField == null) {
    errorField = TheForm.zip;
    }

    }
    if (TheForm.phone.value.length == 0) {
    msg += " * Please enter your phone number.\n";
    if (errorField == null) {
    errorField = TheForm.phone;
    }

    }
    if (TheForm.message.value.length == 0) {
    msg += " * Please enter where did you find our site.\n";
    if (errorField == null) {
    errorField = TheForm.message;
    }
    }

    if (errorField != null) {
    alert(msg);
    errorField.focus();
    return false;
    }

    submitted=1;
    }
    else {
    alert('The form has already been successfully submitted.');
    return false;
    }
    }
    </script>



    HEAD;

    $content = <<< CONTENT

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td style="width:375px;border:0px;border-right:10px;border-color:#FFFFFF;border-style:solid;" align="left" valign="top"><img src="assets/imgs/contactus_photo.jpg" width="375" height="432"></td>
    <td style="background-color:#FF0099;" align="center" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin:10px;">
    <form action="action.php" method="post" onSubmit="return checkForm(this);">
    <tr>
    <td style="padding-top:20px;padding-bottom:10px;font-size:11px;font-weight:bold;">Please
    join our mailing list:</td>
    </tr>
    <tr>
    <td align="center" valign="top">
    <table width="75%" border="0" cellspacing="4" cellpadding="0" align="center">
    <tr>
    <td class="form-field-name-cell" nowrap>Name:</td>
    <td>
    <input type="text" name="name" class="form-field">
    </td>
    </tr>
    <tr>
    <td class="form-field-name-cell" nowrap>Email:</td>
    <td>
    <input type="text" name="email" class="form-field">
    </td>
    </tr>
    <tr>
    <td class="form-field-name-cell" nowrap>Company:</td>
    <td>
    <input type="text" name="company" class="form-field">
    </td>
    </tr>
    <tr>
    <td class="form-field-name-cell" nowrap>Address:</td>
    <td>
    <input type="text" name="address" class="form-field">
    </td>
    </tr>
    <tr>
    <td class="form-field-name-cell" nowrap>State / Zip</td>
    <td>
    <input type="text" name="zip" class="form-field">
    </td>
    </tr>
    <tr>
    <td class="form-field-name-cell" nowrap>Phone:</td>
    <td>
    <input type="text" name="phone" class="form-field">
    </td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td style="padding-left:2px;vertical-align:top;font-size:11px;">Message/Where did you find our site?</td>
    </tr>
    <tr>
    <td style="padding-top:10px;" valign="top" align="center"><textarea name="message" style="width:260px;height:100px;"></textarea></td>
    </tr>
    <tr>
    <td style="padding-top:20px;padding-left:20px;">
    <input type="image" border="0" name="imageField" src="assets/imgs/send_brown.gif" width="71" height="26">
    <input type="image" onClick="this.form.reset();return false;" border="0" name="imageField2" src="assets/imgs/clear_brown.gif" width="71" height="26">
    </td>
    </tr>
    </form>
    </table>
    </td>
    </tr>
    </table>
    CONTENT;

    include_once("page.php");
    ?>

  • #6
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    alert('The form has already been successfully submitted.');
    return 
    false;
    }
    }
    </
    script>
    HEAD
    Why use heredoc? It is an awful form of coding.


  •  

    Posting Permissions

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