morfasto Posted August 20, 2011 Share Posted August 20, 2011 Hello, how are you? I'm having some problems when procesing a text input parameter via ajax. This are my codes: ajax.js function objetoAjax(){ var xmlhttp=false; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } return xmlhttp; } function MostrarConsulta(data){ divComentarios = document.getElementById('comments'); ajax=objetoAjax(); ajax.open("POST", data); ajax.onreadystatechange=function() { if (ajax.readyState==4) { divComentarios.innerHTML = ajax.responseText } } ajax.send(null) } video.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="estilo.css" rel="stylesheet" type="text/css" media="screen" /> <script language="JavaScript" type="text/javascript" src="ajax.js"></script> <?php (isset ($_GET['id']))? $id = $_GET['id'] : $id = NULL; session_start(); function connext($host,$user,$password,$BBDD){ $link=mysql_connect($host,$user,$password) or die (mysql_error()); mysql_select_db($BBDD,$link) or die (mysql_error()); return $link; } $link=conectarse("host","user","password","database"); $video = "SELECT * FROM `videos` WHERE `video_id` = '$id'"; $video = mysql_query($video, $link); $comment1 = "select comment_id, comment, name, lname from video_comments, users where video_comments. user_id = users.user_id and video_id = '$id' order by 1 desc"; $comment1 = mysql_query($comment1, $link); ?> <title>Videos</title> </head> <div id="video"> <?php $rs=mysql_fetch_array($video); echo "<object style='height: 200px; width: 400px'>" ."<param name='movie' value='http://www.youtube.com/v/" .$rs['url']. "'>" ."<param name='allowFullScreen' value='true'>" ."<param name='allowScriptAccess' value='always'>" ."<embed src='http://www.youtube.com/v/" .$rs['url']. "' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'>" ."</object>"; ?> </div> <div id="comments"> <?php while($rs=mysql_fetch_array($comment1)) { echo "" .$rs['name']. " " .$rs['lname']. ": " .$rs['comment']. "<br />"; } ?> </div> <div id="add_comments"> <form action="" onsubmit="MostrarConsulta('add_comment.php'); return false"> <input type="text" name="comment" value="" /> <input type="text" name="id" value="40" /> <input type="submit" value="Add" /> </form> </div> </body> </html> add_comment.php: <?php session_start(); $con = mysql_connect("host","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $comment2="INSERT INTO video_comments (comment_id, user_id, video_id, comment) VALUES ('','$_SESSION[user]','$_POST[id]','$_POST[comment]')"; if (!mysql_query($comment2,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> When I add a comment with the add_comment.php, it doesnt recognise the values of $_POST[id] and $_POST[comment], how do I do to ask the values of the text inputs (id and comment) of the video.php with add_comment so that it recognise the values? This are the errors I get: Notice: Undefined index: id in C:\xampp\htdocs\add_comment.php on line 15 Notice: Undefined index: comment in C:\xampp\htdocs\add_comment.php on line 15 Thank you a LOT for your help! Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/ Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 You need to put them in quotes: $_POST['id'] etc... Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259839 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 I did that already, but still doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259854 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Can you output the result of print_r($_POST) This will check if anything is actually being POST'ed. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259857 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 Nothing is being posted. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259862 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Are you getting Javascript errors? Using Firebug in Firefox or Chrome may be able to help? Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259863 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 I'm not getting any Javascript errors, the only errors that I get are because add_comment.php does not recive the id, comment values: Notice: Undefined index: id in C:\xampp\htdocs\add_comment.php on line 15 Notice: Undefined index: comment in C:\xampp\htdocs\add_comment.php on line 15 Someone told me that I'm not sending any value to the MostrarConsulta(data), I'm just sending the add_comment.php, I need to concatenate the values and send them via MostrarConsulta(), how do I do that? I have no idea. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259865 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Um - hopefully this will help: 1. In your AJAX, you have used: ajax.open("POST", data); it should be: ajax.open("POST", url); also you have used: ajax.send(null) when it should be: ajax.send(data) You should be sending data when it;s a POST with ajax.send Wanna try that and let me know how you get on? Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259868 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 It doesnt show me any error, it means that it doesnt even read the add_comment.php because I got no php errors, also, it doesnt add anything to the database, not even blank spaces. I think that I need to send the text-input values with the form. The ajax.js needs to recive the input-text values and send them to the add_comment.php via MostrarConsulta(). Thank you a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259871 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Did u amend the Javascript as I mentioned? Just one thing - when I mention "url" - I obviously mean that you substitute this with wherever add_comment.php is located. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259873 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 Yes, I did all that! I will translate what I got from http://www.forosdelweb.com/f77/problemas-con-valor-del-text-input-procesar-php-937835/#post3964510: "On which part you send those 2 text-input values (id and comment from video.php) that I dont see?. The only thing that you send to the function MostrarConsulta is the url add_comment.php but by nowhere you pass the values. You have to concatenate the 2 variables and its values." Any clues? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259877 Share on other sites More sharing options...
morfasto Posted August 20, 2011 Author Share Posted August 20, 2011 I just fixed the problem using another script that I found thanks of google: http://www.daniweb.com/web-development/php/threads/178496 Thanks to the people that helped. Quote Link to comment https://forums.phpfreaks.com/topic/245261-trouble-when-procesing-the-text-input-values-with-ajax/#findComment-1259888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.