Jump to content

Trouble when procesing the text input values with ajax.


morfasto

Recommended Posts

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.