validate database entry then echo message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spaghetti
    New Member
    • Dec 2006
    • 5

    #1

    validate database entry then echo message

    Hi

    I'm still new to this but I think I'm making progress. Below is a functioning script that inserts 4 fields into a database. After advice from a previous post the form is being processed on the same page. The insert into database works fine but I only want to display the 'thank you message' after data has been entered. Currently it is displayed all the time.

    Here's the script:

    [PHP]// connect to server & open database
    include '../include/config.php';
    include '../include/opendb.php';

    $Name = $_POST['Name'];
    $ADno = $_POST['ADno'];
    $RecFriend = $_POST['RecFriend'];
    $RecEmail = $_POST['RecEmail'];

    // add info into the database
    mysql_query($qu ery);
    $query="INSERT INTO recommended (Name, ADno, RecFriend, RecEmail) VALUES ('$Name','$ADno ','$RecFriend', '$RecEmail')";
    if(!mysql_query ($query)) die(mysql_error ());

    echo "Thank you for your recommendation! ";

    //close db
    include '../include/closedb.php';
    [/PHP]

    Can someone give me an idea where to go from here and confirm (hopefully) that what I have done so far is solid.

    Thanks people.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Now you state that you want the thank you in the same page as the form. But you are showing only the first part of your script!! See my reply to you at http://www.thescripts.com/forum/post2238469-3.html that gives the form structure.

    So if you want advice here, you'd better show the entire script here, not just part of it, because that doesn't say anything on how it is invoked or executed.

    Ronald :cool:

    Comment

    • spaghetti
      New Member
      • Dec 2006
      • 5

      #3
      Sorry... as I said, I'm finding my way around with this. Here's the form. Thanks for looking.

      [HTML]
      <div class="formRec" >
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="Recommend Friend" onsubmit="MM_va lidateForm('Nam e','','R','ADno ','','R','RecFr iend','','R','R ecEmail','','Ri sEmail');return document.MM_ret urnValue">
      <div align="right">< span class="style1">
      <h2>
      <?php include("Recomm endEcho.php"); ?></h2>
      *Your UserName: <br />
      <input name="Name" type="text" maxlength="30" value="<?php print $slusername; ?>"/>
      <br />
      <br/>
      *Your AD Number:<br />
      <input name="ADno" type="text" maxlength="30" value="<?php print $slcustom6; ?>" />
      <br />
      <br/>
      *Friend's Name:<br />
      <input name="RecFriend " type="text" maxlength="50"/>
      <br/>
      <br/>
      *Friend's Email:<br />
      <input name="RecEmail" type="text" maxlength="50" />
      <br/>
      <br/>
      </span>
      *All fields are required
      <input name="Submit" type="Submit" value="send" />
      <br />
      <h2>&nbsp;</h2>
      </div></form>[/HTML]

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You still show your code script in 2 pieces, while it is only ONE script.

        You must also sanitize your input data and check the validity of the input fields. You say on your form that all fields are mandatory, but how do you force that?? That is also something you must check.

        So here is the reworked code:[php]<?php
        // connect to server & open database
        include '../include/config.php';

        if (isset($_POST['Submit'])) {
        $Name = trim(strip_tags ($_POST['Name']));
        $ADno = trim(strip_tags ($_POST['ADno']));
        $RecFriend = trim(strip_tags ($_POST['RecFriend']));
        $RecEmail = trim(strip_tags ($_POST['RecEmail']));
        //
        // ------------
        // you'd better verify your input fields before inserting them in the database
        // ------------
        // ...........
        // connect to server and database
        include '../include/opendb.php';
        // add info into the database
        $query="INSERT INTO recommended (Name, ADno, RecFriend, RecEmail) VALUES ('$Name','$ADno ','$RecFriend', '$RecEmail')";
        $res = mysql_query($qu ery)
        or die("Insert error: " . mysql_error());

        echo "Thank you for your recommendation! ";
        //close db
        include '../include/closedb.php';
        }
        else {
        ?>
        <div class="formRec" >
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="Recommend Friend" onsubmit="MM_va lidateForm('Nam e','','R','ADno ','','R','RecFr iend','','R','R ecEmail','','Ri sEmail');return document.MM_ret urnValue">
        <div align="right">< span class="style1">
        <h2>
        <?php include("Recomm endEcho.php"); ?></h2>
        *Your UserName: <br />
        <input name="Name" type="text" maxlength="30" value="<?php print $slusername; ?>"/>
        <br />
        *Your AD Number:<br />
        <input name="ADno" type="text" maxlength="30" value="<?php print $slcustom6; ?>" />
        <br />
        *Friend's Name:<br />
        <input name="RecFriend " type="text" maxlength="50"/>
        <br/>
        <br/>
        *Friend's Email:<br />
        <input name="RecEmail" type="text" maxlength="50" />
        <br/>
        </span>
        *All fields are required
        <input name="Submit" type="Submit" value="send" />
        <br />
        <h2>&nbsp;</h2>
        </div>
        </form>
        </div>
        <?php } ?>[/php]
        Ronald :cool:

        Comment

        • moishy
          New Member
          • Oct 2006
          • 104

          #5
          Why don't you use a simple "if(...){echo.. .}"?

          Comment

          • spaghetti
            New Member
            • Dec 2006
            • 5

            #6
            Thanks for that - I will try to implement it. I have the following validation already in place - which was generated by dreamweaver's inbuilt form validator - it seems to function fine but probably isn't the best way to do this... but hey, there's only so many hours in a day!..

            I appreciate the advice!

            [HTML]function MM_validateForm () { //v4.0
            var i,p,q,nm,test,n um,min,max,erro rs='',args=MM_v alidateForm.arg uments;
            for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj( args[i]);
            if (val) { nm=val.name; if ((val=val.value )!="") {
            if (test.indexOf(' isEmail')!=-1) { p=val.indexOf(' @');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
            } else if (test!='R') { num = parseFloat(val) ;
            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf(' inRange') != -1) { p=test.indexOf( ':');
            min=test.substr ing(8,p); max=test.substr ing(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
            } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
            } if (errors) alert('The following error(s) occurred:\n'+er rors);
            document.MM_ret urnValue = (errors == '');
            }[/HTML]

            Comment

            Working...