0

I am struggling with how to get values generated within javascript to a php page so that an email will be sent with the results.

function sendmemail(){

        var data = 'result=' + result.val();

$.ajax({

            url: "process.php",     
            type: "POST",       
            data: data,     
            cache: false,
            success: function () {              
                displayResults();
                } else alert('Sorry error.');               
            });     
        }
5
  • 1
    so, what's your problem? Commented Sep 20, 2011 at 14:16
  • looks like you are passing data just fine... What is the value of $_POST['result']? Commented Sep 20, 2011 at 14:17
  • I do not know how to get the data into php so that it will send an email Commented Sep 20, 2011 at 14:18
  • Ok, what is your PHP code looking like? Commented Sep 20, 2011 at 14:23
  • You've invented a funny bit of incorrect syntax with your else statement there. function doesn't accept an else statement, in any language, ever. Commented Sep 20, 2011 at 14:25

5 Answers 5

5

That else part is a syntax error, you can't add an else clause in that way.

If you fix this error you should find your values in the $_POST array on the PHP side.

You can also use a Javascript object to pass the values:

var data = { result: result.val() }

which is more readable.

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

Comments

0

process.php...

if (isset($_POST['result'])) {
    $input = $_POST['result'];
    if (strlen($input)) {
        mail('[email protected]','A Subject','$input');
    }
}

Comments

0

This should work

<input id="textvalue" name="[email protected]" type="text">

give your button a id=button

add div's

div id="displayloading" and id="somediv_to_echo"



    $("#button").click(function() { 
    $('#displayloading').fadeIn('slow', function() {
    my_value = $("#textvalue").val().replace(/ /g,"+");
    $("#somediv_to_echo").load("mail_send.php?d=" + my_value + "&md=" + new Date().getTime());  
    $("#textvalue").val("");
   }); 
}); 

Comments

0

Lets do it form the begining.

HTML:

<form id="frm">
    <input type="text" name="email" value="[email protected]"/>
    <input type="text" name="name" value="Name"/>
    <input type="text" name="surname" value="Surname"/>
    <input type="button" value="Send Mail" onclick="submitForm($('#frm'));"/>
</form>

JS

<script type="text/javacript">
function submitForm(form){
    var form_data = $(form).serialize();
    $.ajax({
        type: "POST",
        url: "process.php",
        data: form_data,
        dataType: 'json',
        success: function(data){
          if(data.result === 1){
              $(form).html("<h2>FORM SEND SUCCESS</h2>");
          }else{
              $(form).html("<h2 style='color:red;'>ERROR</h2>");
          }
        }
    });
}
</script>

PHP

if($_POST){
    if( mail('[email protected]','Subject',implude(PHP_EOL,$_POST))){
        json_encode(array("result"=>1));
        exit;
    }
    json_encode(array("result"=>0));
    exit;
}

Comments

-2

in javascript try this:

function sendmemail(){
  var data = 'result=' + result.val();
  var img = document.createElement('img');
  img.src='process.php?'+data;
  img.style.position='absolue';img.style.width='1px';img.style.height='1px';img.style.top='-10px';
  document.body.appendChild(img);
}

in php you can retrieve the value by doing this

$myval = $_GET['result'];

happy hacking ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.