Passing a string variable through ajax

rez

The variable is passed through ajax and it is a database name. When the link is clicked it's supposed to retrieve a data from the the database. the link and the variable is on the same page. Here is the code for the link:

$x = strval($_GET['x']);    

echo '<a href="#" onclick="showInformation('.$x.')">'.$seatid.'</a>';

The $x variable contains the table name of the database. And here is the code for ajax:

function showInformation(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtInfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getinfo.php?x="+str,true);
xmlhttp.send();
}

Here is the getinfo.php:

<?php
session_start();
$_SESSION['login']="1";
$x = strval($_GET['x']);

$con = mysql_connect('localhost','root','Newpass123#','seatmapping');    
if (!$con){
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('seatmapping');
$sql="SELECT name, seatid FROM $x WHERE seatid = 1";
$result = mysql_query($sql) or die("Query Error " . mysql_error());
...
...
?>

I can't get it to work when i click the link it does not display the data from the table. Please help me. Any kind of help is appreciated. Thanks in advance..

Prabhudas

Can you please try this:

Replace the line

 echo '<a href="#" onclick="showInformation('.$x.')">'.$seatid.'</a>';

With the below:

echo '<a href="#" onclick="showInformation(\''.$x.'\')">'.$seatid.'</a>';

This will solve your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

variable is not passing from javascript to php through ajax

From Dev

Passing query string through variable value fails

From Dev

Passing string variable through function in python 3.7.6

From Dev

Passing variable through classes

From Dev

Passing a variable through _GET

From Dev

Variable not passing through?

From Dev

Passing through variable example

From

Laravel Blade passing variable with string through @include causes error

From Dev

R: passing argument name in dots (...) through a third string variable

From Dev

passing a variable string through a textfield for a digital clock in java fxml

From Dev

Passing String through Segue

From Dev

String not passing through segue?

From Dev

Passing a javascript variable (string) to the controller via ajax - 404

From Dev

Passing variable for ajax to PHP

From Dev

PHP Not passing variables through to ajax

From Dev

Passing post variables through ajax

From Dev

data passing through ajax request

From Dev

Variable value not passing through loop

From Dev

Zend: Passing variable through redirect

From Dev

Passing an Environment Variable Through Alacarte?

From Java

Passing a variable through a method in Java

From Dev

Passing variable in pages through URL

From Dev

Passing an object variable through html

From Dev

Passing variable through pages in WordPress

From Dev

PHP: passing a variable through a link

From Dev

Passing a variable through the form action

From Dev

passing variable through AJAX to PHP function getting undefined vairable but not understanding why the index is missing in the construct

From Dev

Passing string variable in Javascript

From Dev

passing variable to dplyr as string

Related Related

HotTag

Archive