Jump to content

Having a form insert the data in the database.


laflair13

Recommended Posts

I almost have it but I am missing something. The form is being sent and the row is being created but there is not data. It is blank. I know it is something simple I am missing but I cannot figure it out

 

If anyone can look at my code below and tell me what I am missing to make the inputed info be seen, I would sure appreciate it.

<?php

include_once('class/class_email.php');

// contact to database
$connect = mysql_connect("localhost", "admin", "password") or die ("Error , check your server connection.");

mysql_select_db("database");

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$company = $_POST['company'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$EID = $_POST['eid'];
extract ($_POST);

// Pick up the form data and assign it to variables
// 
$id = intval($id);
$fname = strip_tags($fname);
$lname = strip_tags($lname);
$email = strip_tags($email);
$company = strip_tags($company);
$telephone = intval($telephone); 


$query="INSERT INTO users(`id`, `fname`, `lname`, `email`,`company`,`telephone`) VALUES('$id','$fname','$lname','$email','$company','$telephone')";

echo $query;

mysql_query($query)  or die(mysql_error());
echo mysql_error();

$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `id`='$EID' LIMIT 1;";
$result = mysqli_query($connect,$SQL_GetEquipment);
$row = mysqli_fetch_assoc($result);
$EmailBody = "$fname $lname has requested a quote from NAPE on Item $EID\n 

Information on quote request: \n
Name: $fname $lname \n
Email: $email \n
Company: $company \n
Number: $telephone  \n 
Comments: $comments  \n 
\n
Information Requested for: {$row['itemname']}\n
The URL to {$row['itemname']} is: http://www.mydomain.com.com/new-product.php?Item=$EID
\n
Click to send a quote now:\n
http://www.mydomain.com.com/Admin/send-quote.php?id=$EID ";

$e = new email();

//First value is the URL of your server, the second the port number
$e->set_server( 'mail.mydomain.com.com', 26);

//First value is your username, then your password
$e->set_auth('noreply@mydomain.com', '112233');

//Set the "From" setting for your e-mail. The Name will be base64 encoded
$e->set_sender( 'Quote Requested', 'noreply@mydomain.com' );

//for one recipient
//$send_to = array('myemail@mydomain.com','myemail2@mydomain.com');
$send_to = ('myemail@gmail.com');
//you may also specify multiple recipients by creating an array like this:
//$send_to = array('foo1@localhost.local', 'foo2@localhost.local', 'foo3@localhost.local');


$subject = 'Quote Request from NAPE';
$body = "$EmailBody";
if( $e->mail($send_to, $subject, $body, $headers) == true )
{
  //message was received by the smtp server
  //['last'] tends to contain the queue id so I like to save that string in the database
  echo 'last: '.htmlspecialchars($e->srv_ret['last']).'';
}else{
  //something went wrong
  echo 'all: '.nl2br(htmlspecialchars($e->srv_ret['all'])).'';
  echo 'full:'.nl2br(htmlspecialchars($e->srv_ret['full'])).'';
}

?>
Link to comment
Share on other sites

Here is the code for the form and no I dont know how to echo the post. I am brand new to php

<form id="contact" name="contact" action="#" method="post" style="width:600px">
            <br />
            <table width="80%">
                <tr>
                    <td width="36%">*First Name:</td>
                    <td width="3%"> </td>
                    <td width="61%">
                        <input type="text" id="fname" name="fname" />
                    </td>
                </tr>
                 <tr>
                    <td width="36%">*Last Name:</td>
                    <td width="3%"> </td>
                    <td width="61%">
                        <input type="text" id="lname" name="lname" />
                    </td>
                </tr>
                <tr>
                    <td width="36%">Company Name:</td>
                    <td width="3%"> </td>
                    <td width="61%">
                        <input type="text" id="company" name="company" />
                    </td>
                </tr>

                <tr>
                    <td>*Your E-Mail:</td>
                    <td> </td>
                    <td>
                        <input type="text" id="email" name="email" />
                    </td>
                </tr>
                <tr>
                    <td width="36%">*Phone Number:</td>
                    <td width="3%"> </td>
                    <td width="61%">
                        <input type="text" id="telephone" name="telephone" />
                    </td>
                </tr>
                <tr>
                    <td width="36%" h>Comments:</td>
                    <td width="3%"> </td>
                    <td width="61%">
                        <textarea id="comments" name="comments" cols="25" rows="3"></textarea>
                    </td>
                </tr>
                <tr>
                    <td colspan="3"> </td>
                </tr>
                <tr>
                    <td width="36%" align="center" colspan="3">
                        <button id="send">Request Quote</button>
                    </td>
                </tr>
                <tr>
                    <td colspan="5"> </td>
                </tr>
                <tr>
                    <td width="100%" colospan="3">
                        <b><?php echo $itemname; ?></b>
                        <br />
                        <br /> Manufacturer: <?php echo $manufactuer;?>
                        <br /> Model: <?php echo $model;?>
                        <br /> Category: <?php echo $category;?>
                        <br />
                    </td>
                </tr>
            </table>
        </form>
    </div>

    <!-- basic fancybox setup -->
    <script type="text/javascript">
        $(document)
            .ready(function () {
                $(".modalbox").fancybox();
                $("#contact").submit(function () {
                        return false;
                    });
                $("#send").on("click", function () {
					
                        {
                            // if both validate we attempt to send the e-mail
                            // first we hide the submit btn so the user doesnt click twice
                            $("#send").replaceWith("<em>Your request has been sent...</em>");

                            $.ajax({
                                type: "POST",
                                url: "AJAX_Quote.php",
                                data: JSON.stringify({fname: $("#fname").val(),lname: $("#lname").val(),email: $("#email").val(),eid: $("#Form_ID").val(),company: $("#company").val(),telephone: $("#telephone").val(),comments: $("#comments").val()}),
                                
								dataType: 'json',
                                success: setTimeout(function () { parent.$.fancybox.close(); }, 2000) 
                                    
										
                                        
                                                
                                           
                                    
                                
                            });
                        }
                    });
            });
    </script>
Link to comment
Share on other sites

Ok, I believe I have changed it all to mysqli but it still is coming up blank on the from and in the database

<?php

include_once('class/class_email.php');

$con = mysqli_connect("localhost","admin","password","database");

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$company = $_POST['company'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$EID = $_POST['eid'];

//$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `id`='$EID' LIMIT 1;";
//$R_GetEquipment = mysql_query($SQL_GetEquipment, $Link);
//$row = mysql_fetch_assoc($R_GetEquipment);
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `id`='$EID' LIMIT 1;";
$result = mysqli_query($connect,$SQL_GetEquipment);
$row = mysqli_fetch_assoc($result);
$EmailBody = "$fname $lname has requested a quote from NAPE on Item $EID\n 

Information on quote request: \n
Name: $fname $lname \n
Email: $email \n
Company: $company \n
Number: $telephone  \n 
Comments: $comments  \n 
\n
Information Requested for: {$row['itemname']}\n
The URL to {$row['itemname']} is: http://www.domain.com/new-product.php?Item=$EID
\n
Click to send a quote now:\n
http://www.domain.com/Admin/send-quote.php?id=$EID ";

$e = new email();

//First value is the URL of your server, the second the port number
$e->set_server( 'mail.domain.com', 26);

//First value is your username, then your password
$e->set_auth('noreply@domain.com', 'nape112233');

//Set the "From" setting for your e-mail. The Name will be base64 encoded
$e->set_sender( 'Quote Requested', 'noreply@domain.com' );

//for one recipient
//$send_to = array('me@domain.com','me@gmail.com');
$send_to = ('email@gmail.com');
//you may also specify multiple recipients by creating an array like this:
//$send_to = array('foo1@localhost.local', 'foo2@localhost.local', 'foo3@localhost.local');

$subject = 'Quote Request from NAPE';
$body = "$EmailBody";
if( $e->mail($send_to, $subject, $body, $headers) == true )
{
  //message was received by the smtp server
  //['last'] tends to contain the queue id so I like to save that string in the database
  echo 'last: '.htmlspecialchars($e->srv_ret['last']).'';
}else{
  //something went wrong
  echo 'all: '.nl2br(htmlspecialchars($e->srv_ret['all'])).'';
  echo 'full:'.nl2br(htmlspecialchars($e->srv_ret['full'])).'';
}

mysqli_query($con,"INSERT INTO users (`id`,`fname`,`lname`,`email`,`company`,`telephone`) 
VALUES ('$id','$fname','$lname','$email','$company','$telephone')");


?>
Edited by laflair13
Link to comment
Share on other sites

I appreciate the help and I am trying to figure it out. Can you please tell me how I do the error reporting? 

 

There is no error though, it just isnt posting the info in the email I receive or the database. Its blank

 

And the missing quote was a typo when I deleted the username.

Edited by laflair13
Link to comment
Share on other sites

Another thing, you are assuming everything will work by the way you've written your code. You need to test things with code, like how do you know if your db connection actually connected successfully? How do you know that your queries aren't failing since you don't test for that? You just make a query and move along. Just about everything in mysqli has a method to test to see if it was successful or not before continuing on.

 

http://php.net/manual/en/mysqli.quickstart.connections.php

Check here to see how they test to see if the connection was successful and showing the errors if it wasn't.

 

Same with performing a query:

http://php.net/manual/en/mysqli.query.php

Link to comment
Share on other sites

Where are you submitting your form to? It should be the php file that you posted above. According to your form's action attribute, you are submitting the form data to "#".

<form id="contact" name="contact" action="#" method="post" style="width:600px">
Link to comment
Share on other sites


<!-- basic fancybox setup -->
<script type="text/javascript">
$(document)
.ready(function () {
$(".modalbox").fancybox();
$("#contact").submit(function () {
return false;
});
$("#send").on("click", function () {

{
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$("#send").replaceWith("<em>Your request has been sent...</em>");

$.ajax({
type: "POST",
url: "AJAX_Quote.php",
data: JSON.stringify({fname: $("#fname").val(),lname: $("#lname").val(),email: $("#email").val(),eid: $("#Form_ID").val(),company: $("#company").val(),telephone: $("#telephone").val(),comments: $("#comments").val()}),

dataType: 'json',
success: setTimeout(function () { parent.$.fancybox.close(); }, 2000)







});
}
});
});
</script>
Link to comment
Share on other sites

Sorry, I see you are posting with ajax. I'd suggest trying to get it to work without the ajax and then try adding it in.

 

It would be helpful if you did a print_r($_POST) at the top of your page receiving the form data to see if everything was POSTed correctly.

 

It might also be useful to use jQuery's built in method for building the form data to submit instead of building your own.

data: $('#contact').serialize()
Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.