user6125429
user6125429

Reputation:

$_Post failing (Sending Ajax)

Can someone please show me the correct way I can add these two lines of code

data: {name: info, me: '<?php echo $me; ?>'},

data: dataString ,

So that I can send them in a $_POST to my action.php , I have tried several ways to do this but cannot get both of them successfully to be passed on to my action_comments.php I understand I'm missing something possible when using data: below or have not correctly formatted my code . I'm a total beginner with very little experience so sorry if I lack good practice but hopefully can be better from my debugging . Thanks to anyone who helps me get passed this .

Here is complete code to give overview what Im doing

<script type="text/javascript">

$(function() {  
 // call the submit button ref: name
$(".submit_button").click(function() { 
declare textcontent 
var textcontent = $("#content").val(); 
//dataString = 'content' globel var
var dataString = 'content='+ textcontent; 
declare info 
var info = $('#name').val();
// option no text inputted
if(textcontent=='')
{
// show message if no text 
alert("Enter some text..");
$("#content").focus();
}
else
{       
//display text animation loading
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
//var info equals the string

var info = $('#content').val();
//start ajax call
$.ajax({    
type: "POST",
//path to my action.php
url: "actions/action_comment.php",

//Need to undestand how to correctly format these lines so 
//they are both succesful when submitted to my action_comment.php
$me is declared (not-shown here it holds integer)
data: {name: info, me: '<?php echo $me; ?>'},

// pass the string from textarea to $_POST
data: dataString ,
// I can get one or the other to work but not both

cache: true,

// feed the success my data
success: function(html){    

$("#show").after(html);

document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();  

}  
});
}

return false;
});
});

</script>

I have my $_POST as follows in action_comment.php

echo $me = $_POST['me'];


//DATASTRING FROM TEXTAREA  
echo $content= $_POST['content'];

Upvotes: 0

Views: 44

Answers (1)

Anshum Chauhan
Anshum Chauhan

Reputation: 86

var dataString = 'content='+ textcontent;

$.ajax({    
type: "POST",
url: "actions/action_comment.php",
data: {name: info, me: '<?php echo $me; ?>',txt_data: dataString},
....
});

Cannot use data attribute multiple times in same ajax request. Within php file you can access like $_POST['txt_data'] to get textarea content and same way for other parameters;

define data attribute once and pass all the data like as shown above.

if you want to post whole form data you can use this way

var form = $('#my_form');
  $.ajax( {
      type: "POST",
      url:  form.attr( 'action' ),
      data: form.serialize(),
      ..
      ..
      });

Upvotes: 1

Related Questions