Im trying to send a value of a dropdown on change to a php script. But with my way of solving the problem, the form and also the status String are posted twice. Once with GET parameter set, and the other without. I dont know how to solve,but maybe you are smarter than me.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="js/jquery.ennui.contentslider.js"></script>
<script type="text/javascript">
function showUser()
{
var users = document.getElementById('users').value;
if (users=="" )
{
document.getElementById("pictab").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","slider.php?users="+users,true);
xmlhttp.send();
xmlhttp.reload();
}
<?php
//.............
//..............
//.............
//..............
$soso = mysql_num_rows($connn3);
for($i=0;$i<$soso;$i++)
{
echo "
$(function() {
$('#one$i').ContentSlider({
width : '280px',
height : '180px',
speed : 400,
easing : 'easeOutQuad'
});
});";
}
?>
</script>
<!-- Site JavaScript -->
<form>
<select id="users" name="users" onChange="showUser()" >
<option value="bikes">Bikes</option>
<option value="zub">Stuff/option>
<option value="sonst">Other</option>
</select>
</form>
<br>
<div id="txtHint"></div>
<?php
if(isset($_GET['users'])){
echo "<h2>Q posted</h2>";
$q = $_GET['users'];
echo $q;
//DB QUERY WITH Q
}elseif(!isset($q)){
echo "KEIN Q GEPOSTET";
// DB QUERY WITHOUT Q
}
?>
You've included Jquery into your project so use its features. Mostly Jquery Ajax to handle Ajax requests.
$(function (){ //document is loaded so we can bind events
$("#users").change(function (){ //change event for select
$.ajax({ //ajax call
type: "POST", //method == POST
url: "slider.php", //url to be called
data: { users: $("#users option:selected").val()} //data to be send
}).done(function( msg ) { //called when request is successful msg
//do something with msg which is response
$("#txtHint").html(msg); //this line will put the response to item with id `#txtHint`.
});
});
});
The php part of code should be in slider.php. Moreover, in example I use POST but if you want GET simply change type: "GET"
. In script.php to get value of it use:
$_POST['users'];
or if you change the type to GET then $_GET['users']
you can also use $_REQUEST['users']
which handles POST, GET, COOKIE.
I think this is because your are actually making 2 calls. One in Javascript to the PHP file and the other your echoing no matter what is set. I would comment but haven't got enough juice yet.
Try and echo out $_GET and see what your are actually setting before the call and after the call. From there you may be able to see what's actually going on. I hope this helps guide you in the right direction.
var name=document.forms["myform"]["user"].value;
$.ajax({
type: "POST",
url: "yourpagenmae.php",
data: { name: user }
,
success: function(msg) {
// alert(msg);
}
});
hope it will help you
//view file
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//use jquery get method
$('#users').change(function() {
//get the user value
var user = $('#users').val();
$.getJSON( "http://localhost/test.php?users="+user, function( data1 ) {
console.log(data1);
//status
if (data1.status) {
alert(data1.q);
//whatever response variable key
$('#txtHint').html(data1.q)
} else {
alert('error'+data1.q);
}
});
});
});
</script>
<!-- Site JavaScript -->
<form>
<select id="users" name="users" >
<option value="bikes">Bikes</option>
<option value="zub">Stuff</option>
<option value="sonst">Other</option>
</select>
</form>
<br>
<div id="txtHint"></div>
//test.php file
<?php
$data = array();
if(isset($_GET['users'])){
//echo "<h2>Q posted</h2>";
$q = $_GET['users'];
$data['status'] = true;
$data['q'] = $q;
}elseif(!isset($q)){
$data['status'] = false;
$data['q'] = 'KEIN Q GEPOSTET';
}
header('Content-type:application/json');
//json response
echo json_encode($data);
?>