php $ _POST无法正常工作

时间:2014-04-23 22:08:23

标签: php mysql ajax post

我的表单看起来像enter image description here

我正在使用AJAX。当我选择'Carlos Castaneda'或其他选项时,单选按钮会发生变化, 但是当我选择一个单选按钮时,其他AJAX功能(功能kaina(str)),它应该从单选按钮取值,停止工作。

在我的文件 kaina.php

$tiekejas=$_POST["selectas"];

无效

这是我的 index.php 文件

<?php require 'connect.php';?>
<html>
  <head>
   <link rel='stylesheet' type= 'text/css' href='css/styles.css'>
   <link rel='stylesheet' type= 'text/css' href='css/bootstrap.css'>
 <script>
  var xmlHttp;
function tiekejai(str){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var url="tiekejai.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=keistiTiekejus;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}

/**************************************************************/

function kaina(str){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var url="kaina.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=keistiKaina;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}

function keistiTiekejus() { 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("tiekejai").innerHTML=xmlHttp.responseText;
 } 
}

function keistiKaina() { 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("kaina").innerHTML=xmlHttp.responseText;
 } 
}

function GetXmlHttpObject(){
var xmlHttp=null;
try{xmlHttp=new XMLHttpRequest();}
catch (e){
try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
 }
return xmlHttp;
}
</script>
</head>

<body>

<main id="content" role="content">
 <div id="forma">
<form class="form-horizontal">
<fieldset>

<legend>Uzsakymo forma</legend>

<div class="control-group">
  <label class="control-label" for="selectbasic">Pasirinkite preke</label>
  <div class="controls">
    <select id="selectas" name="selectas" class="input-xlarge" onchange="tiekejai(this.value)">       
  <?php 
    $result = mysqli_query($con,"SELECT * FROM prekes
                    INNER JOIN tiekeju_prekes
                    ON prekes.id = tiekeju_prekes.id_prekes
                    GROUP BY preke");

       while($row = mysqli_fetch_array($result)) {     
       echo "<option value=".$row['id_prekes'].">".$row['preke']."</option>";}      
  ?> 
    </select>
  </div><!--.controls -->
</div><!--.control-group -->

<div class="control-group">
  <label class="control-label" for="radios">Pasirinkite tiekeja</label>
  <div class="controls" id="tiekejai">

  <!--creating radio buttons from different file with AJAX-->

  </div>
</div>

<div class="control-group">
  <label class="control-label" for="radios">kaina</label>
  <div class="controls">

<span class="uneditable-input" id="kaina">Prekes kaina</span>
  </div>
</div>

<div class="control-group">
  <label class="control-label" for="textinput">Kiekis</label>
  <div class="controls">
    <input id="textinput" name="textinput" placeholder="max 40" class="input-xlarge" type="text">
  </div>
</div>

<div class="control-group">
  <label class="control-label" for=""></label>
  <div class="controls">
    <button id="" name="" class="btn btn-primary">Uzsakyti</button>
  </div>
</div>
</fieldset>
</div>
</main>
  <script>
  document.getElementById("selectas").selectedIndex = -1;
  </script>
</body>
</html>

这是我的 tiekejai.php 文件

<?php
$q=$_GET["q"];

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

mysql_select_db("univer", $con);

$sql="  SELECT * FROM tiekejai
    INNER JOIN tiekeju_prekes
    ON tiekejai.id = tiekeju_prekes.id_tiekejo
    WHERE tiekeju_prekes.id_prekes= '".$q ."'";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){

       echo "<label class='radio'>";
       echo  "<input name=radios id=radios  value= '". $row['tiekejas'] ."'  type='radio' onchange='kaina(this.value)'>";
       echo $row['tiekejas'];
       echo "</label>";
}
mysql_close($con);
?>

这是我的 kaina.php 文件

<?php
$q=$_GET["q"];

//this is not working
$tiekejas=$_POST["selectas"];



$con = mysql_connect('localhost', 'root', 'pass');
if (!$con){
 die('Could not connect: ' . mysql_error());}
mysql_select_db("univer", $con);

$sql="  SELECT vnt_kaina FROM tiekeju_prekes
    WHERE id_tiekejo= ." $tiekejas ". AND id_prekes= 7" ;


$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
       echo $row['vnt_kaina'] . " LT" ;
}

mysql_close($con);
?>

0 个答案:

没有答案