Validate chat with PHP, AJAX and MySQL

0

In what I find myself validating my chat, I came up with a form that I do not know if it will work yet but it is as follows:

In the chat at the moment to save two data = $ id_user which is the current session and $ id_ar which is the data of the craftsman who entered the product and when the partner enters your message is saved in this way:

INSERT INTO chat (nombre, mensaje, id_user, id_ar) VALUES ('$nombre','$mensaje','$id_user','$id_ar'

All right, the problem now is this:   Notice: Undefined index: id_ar in C:\XAMMP\htdocs\artesa\mensajeria.php on line 4 at the time of sending the message you lose the variable $ id_ar that I store in the form $ _GET, I do not understand why, yet, a colleague sent me the code that I append below:

--- Messenger ---

<?php
    include "head.php";
    session_start();
    $id_ar = $_GET['id_ar'];
?>


<HTML>
    <HEAD>
        <TITLE>Chat Artesania de mi pueblo</TITLE>
      <link rel="stylesheet" type="text/css" href="estilo.css">

        
        <script type="text/javascript">
            function ajax(){
                var req = new XMLHttpRequest();
                
                req.onreadystatechange = function(){
                    if(req.readyState == 4 && req.status == 200){
                        document.getElementById('chat').innerHTML = req.responseText;
                    }
                }
                req.open('GET','chat.php?id_ar=<?php echo $id_ar; ?>',true);
                req.send();
            }
          
         
            setInterval(function(){ajax();}, 1000);
        </script>
        
        
    </HEAD>

    <BODY onload="ajax();">
       <p><?php 
                    if (isset($_SESSION['username']))
                        echo $_SESSION['username']; 
                    else
                        header("location: ../index.html");
                ?></p> 
       <a href="formsArtesa/catalogo.php">Volver a catalogo</a>
       
        <div id="contenedor"> 
            <div id="caja-chat">
                <div id="chat"></div>
            </div>
            <form method="post" action="mensajeria.php">
                <textarea name="mensaje" placeholder="ingresa un mensaje"></textarea>
                <input type="submit" name="enviar" value="ENVIAR">
            </form>
            <?php
            if (isset($_POST['enviar'])){
                $nombre=$_SESSION['username'];
                $mensaje = $_POST['mensaje'];
                $id_user = $_SESSION['id_user'];
            if ($mensaje=="") {
                
                
            }   
              else{   
                $consulta = "INSERT INTO chat (nombre, mensaje, id_user, id_ar) VALUES ('$nombre','$mensaje','$id_user','$id_ar')";
                $ejecutar = $con->query($consulta);
               } 
                
            }
            
            ?>
            
            
        </div>
        
    </BODY>
</HTML>
--- process_chat ----

<?php
session_start();           
include "head.php";

$id_ar = $_GET['id_ar'];
$user = $_SESSION['id_user'];
$consulta = "SELECT * FROM chat WHERE id_user='$user' AND id_ar = '$id_ar' ORDER BY id DESC";
                        $ejecutar = $con->query($consulta);
                        while($fila = $ejecutar->fetch_array()){
                    
     ?>
                    <div id="datos-chat">
                        <span style="color:darkcyan;"><?php echo $fila['nombre'];?>:</span>
                        <span style="color:gray;"><?php echo $fila['mensaje'];?> </span>
                         <span style="float:right;"><?php echo formatearFecha( $fila['fecha']);?></span>
                    </div>
                    <?php } ?>

--------- The code from where $ _GET starts in case they want ----------

<!DOCTYPE HTML>
<html>
	<head>
		<title>Artesanías de mi pueblo</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<link rel="stylesheet" href="../assets/css/main.css" />
        <link rel="stylesheet" href="assets/css/estilo.css">
        <link rel="stylesheet" href="assets/css/modal.css">
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
	</head>
	<body>
			<div id="wrapper">
					<header id="header">
						<div class="inner">
								<a href="index.html" class="logo">
									<span class="symbol"><img src="../logo.png" alt="" /></span><span class="title">Artesanías de mi pueblo.</span>
								</a>
								<nav>
									<ul>
										<li><a href="#menu">Menú</a></li>
									</ul>
								</nav>
						</div>
					</header>
					<nav id="menu">
						<h2>Menu</h2>
						<ul>
							<li><a href="../formsArtesa/catalogo.php">Inicio</a></li>
							<li><a href="catalogo.php">Categorías</a></li>
							<li><a href="quienessomos.php">¿Quiénes Somos?</a></li>
                            <hr>
                            <h2><?php 
                    session_start();
                    if (isset($_SESSION['username']))
                        echo $_SESSION['username']; 
                    else
                        header("location: ../index.html");
                ?></h2>
                            <li><a href="perfilusuario.php">Configurar Cuenta</a></li>
                            <li><a href="preciodeventa.php">Calculadora Artesa</a></li>
                            <li><a href="../poo.php">Cerrar Sesión</a></li>
						</ul>
					</nav>
						<?php
					$idproducto=$_GET['id_pro'];					
					include '../head.php';
					$consulta="SELECT * FROM producto p INNER JOIN usuarios U on p.id_user=u.id_user WHERE id_pro='$idproducto;'";
					$ejecutar = $con->query($consulta);
					while ($row=$ejecutar->fetch_array()):
						$nombre = $row['nombre_pro'];
						$image = $row['image_pro'];
						$descripcion = $row['descripcion'];
						$precio = $row['precio'];
						$departamento = $row['departamento'];
						$id_user = $row['id_user'];
						$categoria_pro = $row['categoria'];
						$nombre_user = $row['nombre'];
						$user_name = $row['username'];
						$telefono = $row['telefono'];
						$email = $row['email'];
					endwhile;
					 ?>
					<div id="main">
						<div class="inner">
                            <div id="c-slider">
                                <div id="slider">
                                    <div class="slides-container">
                                        <section>
                                            <img src="<?php echo $image; ?>">
                                        </section>
                                    </div>
                                </div>
                            </div>
                            <div class="info">                       
                                <div class="titulo">
                                <h2><?php echo $nombre; ?></h2>
                                </div>
                                <div class="precio">
                                    <p>Precio: </p>
                                    <h3>$<?php echo $precio; ?></h3>
                                </div>
                                <div class="descripcion">
                                    <h3>Descripción: </h3>
                                    <p><?php echo  $descripcion; ?></p>
                                </div>
                                    <p>Contactarse con el artesano:</p>
                                    <input id="mostrar-modal" name="modal" type="radio" />
                                    <label  for="mostrar-modal">Contactar</label>
                                <a href="../mensajeria.php?id_ar=<?php echo $id_user; ?>">Enviar Mensaje</a>

                                    <input id="cerrar-modal" name="modal" type="radio" /> 
                                    <label for="cerrar-modal"> X </label> 
                                    <div id="modal">
                                        <div class="contenido">

                                    <h3>Nombre artesano:</h3>
                                    <p href="perfilusuario.php?id_user=<?php echo $id_user; ?>"><?php echo $nombre_user; ?></p>
                                    <h3>Teléfono: </h3>
                                        <p><?php echo $telefono; ?></p>
                                    <h3>Dirección: </h3>
                                    <p><?php echo $departamento; ?></p>
                                            <h3>Correo: </h3>
                                            <p><?php echo $email; ?></p>
                                            </div>
                                    </div>
                            </div>
                            <div class="regresar">
                                    <a href="Catalogodecatalogo.php?categoria=<?php echo $categoria_pro;?>">Regregar al Catálogo</a>
                            </div>
                            </div>
                    </div>
              
               
                <form  method="post">
        <input type="text" placeholder="comentario" name="comentario"><br>
        <input type="submit" value="comentar!" name="comentar" ><br>
        </form>
        <?php
        require("head.php");
            if (isset($_POST["comentar"])){
                $name=$_SESSION['username'];
                $commentary=$_POST["comentario"];
                $idproducto=$_GET['id_pro'];
            if ($commentary==""){
                echo "<p>Escribe un comentario</P>";
            }
                else{
               $sql="INSERT INTO comentarios (nombre, comentario, producto) VALUES ( '$name', '$commentary','$idproducto')";
                $resultado=mysqli_query($con,$sql); 
            }
            
                
                
            }
        $sql2="SELECT * FROM comentarios WHERE producto=$idproducto";
        $resultado2=mysqli_query($con,$sql2);
        while($fila=mysqli_fetch_array($resultado2,MYSQLI_BOTH)){
            echo "  " . $fila[1] . "<br>" ;
            echo  " Comentario: " . $fila[2] . "<br>";
                
        }
        ?>
                
					<footer id="footer">
						<div class="inner">
							<section>
								<div class="terminos">
                                    <h3>TÉRMINOS Y CONDICIONES</h3>
                                    <p>Artesanías de mi pueblo no se hace responsable de ...
                                    </p>
                                    <p>Para conocer el reglamento y uso del sitio web, 
                                    por favor dirigete a la sección de "¿Quiénes somos?"</p>
                                </div>
							</section>
							<section>
								<h2>Síguenos en </h2>
								<ul class="icons">
									<li><a href="https://twitter.com/artesa_sv" class="icon style2 fa-twitter"><span class="label">Twitter</span></a></li>
									<li><a href="https://www.facebook.com/Artesan%C3%ADas-de-mi-pueblo-1869465476709706/" class="icon style2 fa-facebook"><span class="label">Facebook</span></a></li>
									<li><a href="https://www.instagram.com/artesa_sv/" class="icon style2 fa-instagram"><span class="label">Instagram</span></a></li>
									<li><a href="#" class="icon style2 fa-phone"><span class="label">Phone</span></a></li>
									<li><a href="#" class="icon style2 fa-envelope-o"><span class="label">Email</span></a></li>
								</ul>
							</section>
							<ul class="copyright">
								<li>&copy; Copyright 2017  |  Artesanias de mi pueblo </li>
							</ul>
						</div>
					</footer>

			</div>
            <script src="assets/css/slider.js"></script>
			<script src="assets/js/jquery.min.js"></script>
			<script src="assets/js/skel.min.js"></script>
			<script src="assets/js/util.js"></script>
			<script src="assets/js/ie/respond.min.js"></script>
			<script src="assets/js/main.js"></script>
            <script src="assets/js/jquery.js"></script>
        <script src="assets/js/jquery.superslides.js"></script>
	</body>
</html>
    
asked by Luis Rivera 19.08.2017 в 23:11
source

0 answers