Page 1 of 1
Newb question - Data does not get submitted on a form
Posted: Fri Aug 22, 2008 3:15 am
by keropikero
Edit: Please refer to post#4, I have made some changes to my codes since then. Thank you.
Inexperienced developer here trying to make a gaming website.
I am asking user to select a few values from drop down list plus fill in a few text boxes and hit submit. Then I want to insert these into a record. A record is created each time but other than the memberid, nothing else is pulling (all blanks). Please help.
Partial code:
Code: Select all
<?
...
switch($action){
case "submitscores":
addscorerecord();
break;
...
}
$q="SELECT * FROM games";
$result=mysql_query($q);
$out[body].="<select name=\"title\" onChange=\"getCat(this.value)\">
<option >Select Game</option>";
$memberid="$_COOKIE[tid]";
while($n=mysql_fetch_array($result)){
$out[body].="\"<option value=$n[gameid]>$n[title]</option>\";";
}
$out[body].="</select>
<div id=\"statediv\"><select name=\"cat\" >
<option>Select Game First</option>
</select></div>
<div id=\"citydiv\"><select name=\"subcat\">
<option>Select Category First</option>
</select></div>
<form name=\"score\"><input type=text name=score value=$score>
</form>
<form name=\"URL\"><small>http://</small><input type=text name=url value=$url></form>
<form name=\"date\"><input type=date name=date value=$date></form>
<input type='hidden' name='action' value='submitscores'>
<input type='hidden' name='cat' value='$cat'>
<input type='hidden' name='subcat' value='$subcat'>
<input type='hidden' name='score' value='$score'>
<input type='hidden' name='url' value='$url'>
<input type='hidden' name='date' value='$date'>
<input type='hidden' name='title' value='$title'>
<input type='submit' class='button' name='submit' value='Submit >>'>
</form>
";
include("$config[html]");
}
function addscorerecord(){
include("./includes/incglobal.php");
global $config;
}
mysql_query("INSERT INTO scorerecords VALUES (
NULL,
'$_COOKIE[memberid]',
'$cat',
'$score',
'$url',
'$date',
'',
'0')");
Re: Newb question - Data does not get submitted on a form
Posted: Fri Aug 22, 2008 12:34 pm
by keropikero
sorry for the bump
anyone? please help
Re: Newb question - Data does not get submitted on a form
Posted: Fri Aug 22, 2008 8:52 pm
by califdon
Yes, for future reference, we ask that members wait at least 24 hours before "bumping". The way most of us operate here, using "View New Posts", we will see your post just as quickly without any "bumping". All you do by "bumping" is annoy some of the people who are most likely to help you.
You are not storing most of the data probably because you haven't assigned values to those variables ($cat, $score, $URL, etc.). At least not in the code you have shown. Assuming that your form is using the "post" method, you will need to assign values, like:
Code: Select all
...
$cat = mysql_real_escape_string($_POST['cat']);
$score = mysql_real_escape_string($_POST['score']);
$URL = mysql_real_escape_string($_POST['URL']);
...
PHP doesn't automatically assign values to variables, you have to do it. And when you do, and if you're going to be using the values in an SQL string, you must use the mysql_real_escape_string() function to escape certain punctuation so that hackers cannot insert end-of-string characters, for example, that will allow them to break your security.
Re: Newb question - Data does not get submitted on a form
Posted: Sat Aug 23, 2008 12:34 am
by keropikero
Thank you and sorry. I was pulling my hair to figure this one out.
I tired what you suggested and still can't get this to work.
I actually changed around my codes a bit on my own and got all the variables to pass except the one "subcat". I am including the codes here again because #1 - I didn't include everything last time and #2- i changed a few things since then. The cat and subcat variables actually call for another .php file and probably that's why it's different. I have spent 3 days on this now and i know i am not that far away from the end
Thanks again for your help.
Code: Select all
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCat(gameid) {
var strURL="findcat.php?game="+gameid;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('subcatdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getsubcat(gameid,catid) {
var strURL="findsubcat.php?game="+gameid+"&cat="+catid;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('catdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
<?
include("./includes/incglobal.php");
switch($action){
case "submitscores":
addscorerecord($score);
break;
default:
check_login($login);
break;
}
function check_login($login){
not copied here.
include("$config[html]");
exit;
}
$out[body].="
<form method=\"post\" name=\"form1\">
$q="SELECT * FROM games";
$result=mysql_query($q);
$out[body].="<select name=\"title\" onChange=\"getCat(this.value)\">
<option >Select Game</option>";
while($n=mysql_fetch_array($result)){
$out[body].="\"<option value=$n[gameid]>$n[title]</option>\";";
}
$out[body].="</select>
<div id=\"subcatdiv\"><select name='score[cat]' >
<option>Select Game First</option>
</select></div>
<div id=\"catdiv\"><select name='subcat'>
<option>Select Category First</option>
</select></div>
<input type=text name='score[score]' value=''>
<small>http://</small><input type=text name='score[url]' value=''>
<input type=date name='score[date]' value=''>
<input type='hidden' name='action' value='submitscores'>
<input type='submit' class='button' name='submit' value='Submit >>'>
</form>";
/// i tried inserting $subcat = mysql_real_escape_string($_POST['subcat']); here, didn't work
include("$config[html]");
}
function addscorerecord($score){
global $config;
$subcat = mysql_real_escape_string($_POST['subcat']);
//INSERT score INTO DATABASE
mysql_query("INSERT INTO scorerecords values(NULL,
'$_COOKIE[tid]',
'$subcat',
'$score[score]',
'$score[url]',
'$score[date]',
'',
'0')");
include("$config[html]");
}
?>
findsubcat.php
Code: Select all
<?
include("./includes/incglobal.php");
$gameid=intval($_GET['game']);
$catid=intval($_GET['cat']);
$query="SELECT subcatid,subcatname FROM gamessubcat WHERE gameid='$gameid' AND catid='$catid'";
$result=mysql_query($query);
?>
<select name='subcat'>
<option>Select Subcategory</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['subcatid']?>><?=$row['subcatname']?></option>
<? }
/// i tried putting the line here too
?>
</select>
findcat.php
Code: Select all
<?
include("./includes/incglobal.php");
$game=intval($_GET['game']);
$query="SELECT catid,catname FROM gamescat WHERE gameid='$game'";
$result=mysql_query($query);
?>
<select name="cat" onchange="getsubcat(<?=$game?>,this.value)">
<option>Select Category</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['catid']?>><?=$row['catname']?></option>
<? } ?>
</select>
Re: Newb question - Data does not get submitted on a form
Posted: Sat Aug 23, 2008 3:59 pm
by califdon
Well, no, it didn't work because you weren't receiving values from a form POST method. You seem to be using a script that is using AJAX to populate the variables. It is a rather lengthy script and depends upon other pieces of code not shown. Without wanting to be unfriendly, I just don't have time to try to debug a long and complicated script, even if it was all there. If you are trying to learn PHP and AJAX, you should start with something less complex. If you are just trying to get somebody else's script working, that's fine, but you'll have to find someone who has the time to help with that.
Re: Newb question - Data does not get submitted on a form
Posted: Sat Aug 23, 2008 10:00 pm
by keropikero
I have no way of coming up with a script like this. This is someone's else script, that I am tailoring for something I thought was relatively simple.
I want a form that lets the user choose from 3 dynamic select lists that's dependent (game, then category, then subcategory), plus a few more fill in text fields, then submit that data into mysql table.
But I also need to fit this into a bigger framework, another script I found somewhere else. It's proving to be very difficult...