Jump to content

wont seem to recognise array values in form checker


real_illusions

Recommended Posts

Hi,I'm trying to make a code checker, based from a username checker (hence the username variables and such)..but it wont seem to work. Whatever I put into the input field, I get "xxxx is not Available", even when it is.So,I have this, all these are on a webpage built into a form, and header includes and everything, but these are the relevant parts. I cant work out why its not reading whats in the array. Eventually, I will have the array populated from a MySQL database, and I swear this was working last night..but now it isn't....and have no idea why. Any clues?HTML (within a bigger form):

<input class="req" type="text" name="discountcode" id="discountcode" /><a href="#" id="discountcodecheck">Check code</a><div id='discountcode_availability_result'></div>

Javascript (jQuery library already included on the page):

<script language="javascript">$(document).ready(function() {		//the min chars for username		var min_chars = 3;		//result texts		var characters_error = 'Minimum amount of chars is 3';		var checking_html = 'Checking...';		//when button is clicked		$('#discountcodecheck').click(function(){			//run the character number check			if($('#discountcode').val().length < min_chars){				//if it's bellow the minimum show characters_error text '				$('#discountcode_availability_result').html(characters_error);			}else{				//else show the cheking_text and run the function to check				$('#discountcode_availability_result').html(checking_html);				check_availability();			}		});  });//function to check username availabilityfunction check_availability(){		//get the username		var username = $('#discountcode').val();		//use ajax to run the check		$.post("codecheck.php", { username: username },			function(result){				//if the result is 1				if(result == 1){					//show that the username is available					$('#discountcode_availability_result').html(username + ' is Available');				}else{					//show that the username is NOT available					$('#discountcode_availability_result').html(username + ' is not Available');				}		});}</script>

PHP (codecheck.php):

<?php//this varible contains the array of existing codes$existing_users=array('code1','code2','code3'); //value got from the get metho$user_name=$_POST['discountcode'];//checking weather user exists or not in $existing_users arrayif (in_array($user_name, $existing_users)){	//code is in the list	echo 1;} else{	//code is not in the list	echo 2;}?>

Link to comment
Share on other sites

The first thing to do is check what is being received from PHP. If you're receiving the right value from PHP, then check to see how your code is using that value.

Link to comment
Share on other sites

as Ingolme has said, have you verified the data through all steps?1) check availability

//get the usernamevar username = $('#discountcode').val();console.log('USERNAME IS => ' + username);

2) codecheck.php

//value got from the get metho$user_name=$_POST['discountcode'];echo $user_name;

3) check availability

function(result){  console.log('RESULT IS => ' + result);  //if the result is 1

it could be that the value are being passed around as string and/or integers. It could just be data type mismatches when doing the comparisons although using == should take care of that...

Link to comment
Share on other sites

It must be something to do with $_POST['discountcode'] in the codecheck.php, but as that file is called up via JS, how do I check what that variable is? Echoing it out breaks the script.If its not getting the value correctly..then how do I go about doing that?

Link to comment
Share on other sites

wait i see it.

$.post("codecheck.php", { username: username },

I'm guessing this is setting a member username in the POST array, to the value of username from your input element.should it not be

$.post("codecheck.php", { discountcode: username },

?basically whatever members of the post array your are initializing, wether it be called username, or discountcode, it must match the value you're looking for on the PHP end. It doesn't matter which one you change, just as long they both match.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...