javascript - Unable to send jQuery variable with string content to PHP using jQuery AJAX function? -
the code snippet jquery function looks like:
function addmessage() { if (textval != "") { text_string='<div class="alert-box round"><p class="text-left">' + username + ':' + textval + '</p></div></br>'; alert(text_string); $.ajax({ type:"post", url:"process.php", data: {'text_string': text_string}, cache:false, success:function(){ alert("submitted") } }); $("input[type=text]:last").val(""); } enterbutton = 0; } the process.php code looks like:
<body> <?php //$host = "localhost"; $text_string=$_post['text_string']; echo "string submitted is".$text_string; ?> </body> i alerts showing value of text_string , "submitted", when open php page, shows error:
undefined index: text_string
i've seen various answers, none of them seem case mine. problem in php code or jquery code or both?
if want save value passed ajax request next time load "process.php", try saving in session. so, change code to:
<?php session_start(); // store value in session if passed if (isset($_post['text_string'])){ $_session['text_string'] = $_post['text_string']; } // read , echo value session if set else if (isset($_session['text_string'])){ $text_string=$_session['text_string']; echo "string submitted is".$text_string; } ?> now, php script store passed value in session, , echo stored value should load page elsewhere. (another alternative store value in database...though i'm not sure if have 1 set @ moment.)
hope helps! let me know if have questions.
Comments
Post a Comment