user3201500
user3201500

Reputation: 1618

post value through ajax of uploaded file in php

We are trying to POST one data string through AJAX here is how it looks

// if CAPTCHA is correctly entered!                       
    if ($resp->is_valid) {  echo $tmpName; ?> 


                      <script type="text/javascript">

$(document).ready(function(){

   alert("in ready");
        var name = "<?php echo $_POST['name']; ?>";

        var email = "<?php echo $_POST['email']; ?>";
        var state = "<?php echo $_POST['state']; ?>";
        var contact = "<?php echo $_POST['phone']; ?>";
         var message = "<?php echo strtr($_POST['message'], array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />')); ?>";


        // Returns successful data submission message when the entered information is stored in database.
       var dataString = 'name1='+ name + '&email1='+ email + '&state1='+ state + '&contact1='+ contact + '&message1='+ message +'&tmpName1='+ "<?php echo $tmpName ?>" +'&fileType1='+ "<?php echo $fileType ?>" +'&fileName1='+ "<?php echo $fileName ?>"; 


       alert(dataString);

        if(name==''||email==''||contact=='')
        {
            alert("Please Fill All Fields");
        }
        else
        {
        // AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "mailer.php",
                data: dataString,
                cache: false,
                success: function(result){
                    alert(result);
                }
            });
        }
        return false;

});

When we are trying to get the value of the tmpName1 but we can not get it but rest like name1 we can get that value. Anyone can suggest on this issue?

Thank you!

Upvotes: 1

Views: 71

Answers (3)

Raham
Raham

Reputation: 4951

You need to get it as you posted it - meaning if you write it like this:

dataString = {name_1:name, course_1:course},

then you must retrieve it using the names you've given: name_1 and course_1, otherwise it will not work.

Upvotes: 0

miglio
miglio

Reputation: 2058

this is the right way:

var dataString = 'name1='+ name + '&email1='+ email + '&state1='+ state + '&contact1='+ contact + '&message1='+ message +'&tmpName1=<?php echo $tmpName ?>&fileType1= <?php echo $fileType ?>&fileName1=<?php echo $fileName ?>';

but this way is better:

var tmpName = '<?php echo $tmpName ?>'||'',
fileType    = '<?php echo $fileType ?>'||'',
fileName    = '<?php echo $fileName ?>'||'';

var params = {
    name1:name,
    email1:email,
    state1:state,
    contact1:contact,
    message1:message,
    tmpName1:tmpName,
    fileType1:fileType,
    fileName1:fileName
}

alert(params);

        if(name==''||email==''||contact=='')
        {
            alert("Please Fill All Fields");
        }
        else
        {
        // AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "mailer.php",
                data: params,
                cache: false,
                success: function(result){
                    alert(result);
                }
            });
        }
        return false;

Upvotes: 0

Cayce K
Cayce K

Reputation: 2338

You could use a different approach for naming the values.

var dvals = {};

dvals = {
    name1: name,
    email1: email,
    ...
};

Personally I don't do that and I'm not 100% sure why, but I do know that I use in my code.

var dvals = {};
dvals['name1'] = name;
dvals['email1'] = email
...

You can use that to name each one for sure and then replace dataString in your AJAX code with dvals. I am not sure why your dataString would be incorrect, but I do know I stopped using it because of random problems I kept running into that made me feel it was unreliable. That is why I switched to using the methods above.

Upvotes: 1

Related Questions