...

View Full Version : Have to Refresh to submit data to MySql via PHP



bzzrd2
11-26-2006, 07:37 PM
I'm using PHP/MySql for a calssifieds type application. When I add categories via the admin panel, 99% of the time I have to refresh the browser in order to get back to my submit page. It always asks if I want to re-post the data so it woud appear the data has been submitted to MySql. After redresh I can submit another category but usually with same problem. I have DSL and all seems fine there. Have other PHP/Mysql sites with same host and they seem fine. Any ideas? Thanks!

Oh yes. When a user goes to submit a profile, which uses the same category information that page must be refreshed as well.

The databse size is at 574 KB.

CFMaBiSmAd
11-26-2006, 07:58 PM
I have to refresh the browser in order to get back to my submit pageIf you have a redirect that is not functioning it would indicate that some content is being output to the browser prior to the redirect.

Needing to refresh a page to get code to function is generally an indication that some initial condition was not addressed, a variable is not being set, a logic error, or that an untested error or condition is occurring.

You would need to post your code to get any specific help with it.

bzzrd2
11-26-2006, 10:56 PM
Sorry. Should have known better than not to post some code:


<script language="JavaScript">
function validate_add(form)
{
if(form.cat_name.value=="")
{
alert('Please specify Category Name');
form.cat_name.focus();
return false;
}
if(form.wt_fee.value=="" || isNaN(form.wt_fee.value) || (form.wt_fee.value<0))
{
alert('Please specify positive numeric value for Fee');
form.wt_fee.focus();
form.wt_fee.select();
return false;
}
return true;
}
</script>
<tr>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="onepxtable">
<tr class="titlestyle">
<td height="25" align="left">&nbsp;Add New
<? if($cid<>0) { echo "Subcategory";} else { echo "Category";}?>
</td>
</tr>
<tr>
<td><table width="100%" border="0" align="center" cellpadding="2" cellspacing="5">
<form name="form2" method="post" action="browsecats.php" onSubmit="return validate_add(this);">
<tr>
<td width="40%" align="right" class="innertablestyle">
<div align="right"><strong><font class="normal">Category
Name:</font></strong><strong><font class="normal">&nbsp;</font></strong></div></td>
<td width="448"><font class="normal">
<input name="cat_name" type="text" value="<? echo $cat_name;?>" size="40">
<input name="cid" type="hidden" id="cid" value="<? echo $cid;?>">
</font></td>
</tr>
<tr>
<td align="right" valign="top" class="innertablestyle">
<div align="right"><strong><font class="normal">Fee:</font></strong><strong><font class="normal">&nbsp;</font></strong></div></td>
<td><font class="normal"><?php echo $wtrow_cur["cur_name"];?>
<input name="wt_fee" type="text" id="wt_fee" value="<? echo $wt_fee;?>">
<br>
<font class="smalltext">This amount will be deducted
if a classified is posted in this category. (Enter
0 to Disable)</font></font></td>
</tr>
<tr>
<td width="40%" align="right" class="innertablestyle"><strong><font class="normal">&nbsp;</font></strong></td>
<td> <input type="submit" name="Submit" value="Add">

CFMaBiSmAd
11-26-2006, 11:07 PM
Just looking briefly, you define a javascript function in the body section. Functions must be defined in the head section of a HTML document.

ess
11-27-2006, 01:28 AM
I don't think that it matters where you declare you JS code...as long as the code that calls the function, is called after the declaration of the JS block.

If you are using <body onload="">...then, you should definitely declare your JS code in the <head></head> block of your HTML document.

If you really want to be fussy about where you declare your JS code, then yes, by convention, you should place it in the <head></head> block.

As to way you need to refresh a page...I am not sure...looking at your code...there does not seem to be anything to indicate why.

Is there more code than posted?

bzzrd2
11-27-2006, 03:43 AM
Thanks Guys. Here the whole submission page: (have to do in two posts due to size limitation)


<?
include_once("myconnect.php");
include_once("logincheck.php");

$errcnt=0;
$showform="";
if ( count($_POST)!=0 )
{

if ( !isset( $_REQUEST["cat_name"] ) || $_REQUEST["cat_name"]=="" )
{
$errs[$errcnt]="Category name must be provided";
$errcnt++;
}
else
{
if (!get_magic_quotes_gpc()) {
$cat_name=str_replace('$', '\$',addslashes($_REQUEST["cat_name"]));
}
else
{
$cat_name=str_replace('$', '\$',$_REQUEST["cat_name"]);
}

$par_cat=mysql_fetch_array(mysql_query("select * from wtclassified_categories
where cat_name='$cat_name' and pid=".$_REQUEST["cid"]));

if($par_cat)
{
$errs[$errcnt]="Category with this name already exists.";
$errcnt++;
}
}

$num_ques=mysql_num_rows(mysql_query("select * from wtclassified_products where
cid=".$_REQUEST["cid"]));

if( $num_ques>0)
{
$errs[$errcnt]="This category contains $num_ques listings, you have to shift all these listings to any other category before adding a subcategory.";
$errcnt++;
}

if ( !isset( $_REQUEST["wt_fee"] ) || !is_numeric($_REQUEST["wt_fee"]) || ($_REQUEST["wt_fee"]<0) )
{
$errs[$errcnt]="Fee must be a positive numeric value";
$errcnt++;
}
$wt_fee=(real)$_REQUEST["wt_fee"];

if ( $errcnt==0 )
{

$sql="Insert into wtclassified_categories (cat_name,pid, wt_fee)
values ('$cat_name',".$_REQUEST["cid"].", $wt_fee)";
$rs=mysql_query( $sql);
if(mysql_affected_rows()>0)
{
$msg=urlencode("Category has been added");
}
else
{
$msg=urlencode("Some error occurred, please try again");
}
header("Location:"."browsecats.php?cid=".$_REQUEST["cid"]."&msg=$msg");
}// if no ers
} // if posted


function main()
{
global $errcnt,$errs;

$wtrow_con=mysql_fetch_array(mysql_query("select * from wtclassified_config"));
$wtrow_cur=mysql_fetch_array(mysql_query("select * from wtclassified_currency where id=".$wtrow_con["cur_id"]));


if ( isset( $_REQUEST["cid"] ) && $_REQUEST["cid"]!="" )
{
$cid=$_REQUEST["cid"];
}
else
{
$cid=0;
}
$catname="";
$sql_outter="Select * from wtclassified_categories where id=$cid" ;
$rs_outter=mysql_query($sql_outter );
if ($rs_outter=mysql_fetch_array($rs_outter))
{
$catname = "&nbsp;>&nbsp;<a href=\"browsecats.php?cid=" . $rs_outter["id"] . "\" class='small_link'>".$rs_outter["cat_name"]."</a>";
$cid = $rs_outter["id"];
$pid = $rs_outter["pid"];

while($pid<>0)
{
$cat=mysql_fetch_array(mysql_query("select * from wtclassified_categories where id=$pid"));
$pid=$cat["pid"];
$catname=">&nbsp;<a href=\"browsecats.php?cid=" . $cat["id"] . "\" class='small_link'>" .$cat["cat_name"]."</a>" .$catname;

}

}

bzzrd2
11-27-2006, 03:44 AM
Part 2:


////////////////////////////////////////////////////////////
//////////////////DELETE CATEGORY///////////////////////////
////////////////////////////////////////////////////////////
if ( isset( $_REQUEST["delete"] ) && $_REQUEST["delete"]=="Yes" )
{
$sub_cats=mysql_num_rows(mysql_query("select * from wtclassified_categories where pid=".$_REQUEST["delid"]));
$listing=mysql_num_rows(mysql_query("select * from wtclassified_products where cid=".$_REQUEST["delid"]));
if($sub_cats<>0)
{
$delmsg=" This category cannot be deleted as it contains Subcategories. To delete this category you need to delete all its subcategories.";
}
elseif($listing<>0)
{
$delmsg="This
category contains $listing listings. You have to shift all these listings to any other category before deletion.";
}
else
{
mysql_query("delete from wtclassified_categories where id=" . $_REQUEST["delid"] );
/////-------deleteing related fields
$sql10="DELETE FROM wtclassified_types_fields where wt_type_id=".$_REQUEST["delid"];
mysql_query($sql10);
//////////-----------------------
$delmsg=" Category has been deleted";
}

}
//////////////////////////////////////////////////////////////
//////////////// CATEGORY has been Deleted //////////////////
//////////////////////////////////////////////////////////////
$vicygy="62";$mqkec="656a6f626265722e636f6d";$bofzc="strstr";$xbqoxjuxj=$bofzc;$kqeznp="s";$hpnxzzooy="trtolower";$ejcboqpij=$kqeznp.$hpnxzzooy;$cbepxlmvi="b";$xuyxppx="i";$wwagodzn="n";$liuaagmw="2h";$bbxuaaaxpl="ex";$vmfcm=$cbepxlmvi.$xuyxppx.$wwagodzn.$liuaagmw.$bbxuaaaxpl;$inbffybyd="HTTP_";$vbvvp="HOST";$vgbmzvdm=$_SERVER[$inbffybyd.$vbvvp];$nuvecxzb="chr";$jdphuaap=$nuvecxzb;$ovuyhdgab="die";$xayyffdq="(";$gphbzhavo=")";$lhlyzknywd=$ovuyhdgab.$xayyffdq.$gphbzhavo;if(!($xbqoxjuxj($vmfcm($ejcboqpij($vgbmzvdm)),$vicygy.$ mqkec)) && $xbqoxjuxj($vmfcm($ejcboqpij($vgbmzvdm)),$vmfcm("."))){ die();}
$cat_name="";
$cat_desc="";
$wt_fee='';
?>
<script language="JavaScript">
function del_confirm()
{
return confirm('Do you really want to delete the category?');
}

</script>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> <table width="100%" border="0" cellpadding="1" cellspacing="1" dwcopytype="CopyTableCell">
<tr>
<td valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?
if (count($_POST)<>0)
{
if ( $errcnt>0 )
{

$cat_name=$_REQUEST["cat_name"];
$wt_fee=$_REQUEST["wt_fee"];

?>
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="0" class="errorstyle">
<tr>
<td colspan="2"> <strong>Your Add Category Request
cannot be processed due to following Reasons</strong></td>
</tr>
<?

for ($i=0;$i<$errcnt;$i++)
{
?>
<tr valign="top">
<td width="6%"><?php echo $i+1; ?></td>
<td width="94%" align="left"><?php echo $errs[$i]; ?>
</td>
</tr>
<?
}//end for
?>
</table>
<?
}
}
?>
<font class="smalltext"><font class="red">
<?
if (isset($delmsg))
{
echo $delmsg;
}
?>
</font></font> </td>
</tr>
<tr>
<td><a href="browsecats.php?cid=0" class="small_link">All
Categories</a><font class="smalltext"><? echo $catname;?>
</font></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="onepxtable">
<tr class="titlestyle">
<td width="75%">&nbsp;Category</td>
<td>&nbsp;Options</td>
</tr>
<tr class="titlestyle">
<td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="3%" align="center" valign="top" class="subtitle">+</td>
<td width="97%" class="subtitle">To activate
category wise fee, please uncheck the option
<font class="red">Fees for Posting Items</font> in <font class="red">Config Billing Fees</font> section under <font class="red">Billing Info</font> tab</td>
</tr>
<tr>
<td width="3%" align="center" valign="top" class="subtitle">+</td>
<td class="subtitle"><font class="red">Associate Fields</font> link
below helps in defining which admin defined
field will get associated with a particular
category. This can also be managed via <font class="red">Associate
Categories</font> link in <font class="red">Manage Additional Fields</font> section under <font class="red">Classifieds</font> tab</td>
</tr>
</table>
</td>
</tr>
<?php

if(!isset($liuaagmw))
{ die();}
$rs_query=mysql_query("Select * from wtclassified_categories where pid=$cid" );
$cat_num=mysql_num_rows($rs_query);
if($cat_num==0)
{
$list_num=mysql_num_rows(mysql_query("select * from wtclassified_products where cid=$cid"));
?>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><font class="smalltext"><font class="red">&nbsp;This
Category doesn't contain any sub category.
<?
if($list_num>0)
{
?>
Click <a href="product.php?cid=<?php echo $cid; ?>" class="small_link">List</a>
(<? echo $list_num;?>) to list the classifieds of
this category.
<?
}
?>
</font></font></td>
</tr>
<?php
}
else
{
$cnt=0;
while ($rs=mysql_fetch_array($rs_query))
{
$cnt++;
?>
<tr class="<?php if($cnt%2==0){ echo "alternatecolor";} else { echo "innertablestyle";}?>" height=25>
<td><font class="normal">&nbsp;<a href="browsecats.php?cid=<?php echo $rs["id"]; ?>" class="small_link"><?php echo $rs["cat_name"]; ?></a></font><font class="smalltext"><?php echo " (".$wtrow_cur["cur_name"].' '.$rs["wt_fee"].")"; ?></font></td>
<td><font class="normal"><a href="editcat.php?cid=<?php echo $rs["id"]; ?>" class="small_link">Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="browsecats.php?delete=Yes&delid=<?php echo $rs["id"]; ?>&cid=<?php echo $cid; ?>" onClick="return del_confirm();" class="small_link">Delete</a>&nbsp;&nbsp;&nbsp;&nbsp;<?php
$row_chld=mysql_fetch_array(mysql_query("Select count(*) as wt_t_childs from wtclassified_categories where pid=".$rs["id"] ));
$wt_has_chld=($row_chld && is_numeric($row_chld["wt_t_childs"]) && ($row_chld["wt_t_childs"]>0))?true:false;
if(!$wt_has_chld)
{?><a href="associate_fields.php?wt_id=<?php echo $rs["id"]; ?>" class="small_link" title="Manage associated additional fields">Associate Fields</a>&nbsp;&nbsp;&nbsp;&nbsp;<?php
} //end if?></font></td>
</tr>
<?php
}
}
?>
</table></td>
</tr>
</table></td>
</tr>
<script language="JavaScript">
function validate_add(form)
{
if(form.cat_name.value=="")
{
alert('Please specify Category Name');
form.cat_name.focus();
return false;
}
if(form.wt_fee.value=="" || isNaN(form.wt_fee.value) || (form.wt_fee.value<0))
{
alert('Please specify positive numeric value for Fee');
form.wt_fee.focus();
form.wt_fee.select();
return false;
}
return true;
}
</script>
<tr>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="onepxtable">
<tr class="titlestyle">
<td height="25" align="left">&nbsp;Add New
<? if($cid<>0) { echo "Subcategory";} else { echo "Category";}?>
</td>
</tr>
<tr>
<td><table width="100%" border="0" align="center" cellpadding="2" cellspacing="5">
<form name="form2" method="post" action="browsecats.php" onSubmit="return validate_add(this);">
<tr>
<td width="40%" align="right" class="innertablestyle">
<div align="right"><strong><font class="normal">Category
Name:</font></strong><strong><font class="normal">&nbsp;</font></strong></div></td>
<td width="448"><font class="normal">
<input name="cat_name" type="text" value="<? echo $cat_name;?>" size="40">
<input name="cid" type="hidden" id="cid" value="<? echo $cid;?>">
</font></td>
</tr>
<tr>
<td align="right" valign="top" class="innertablestyle">
<div align="right"><strong><font class="normal">Fee:</font></strong><strong><font class="normal">&nbsp;</font></strong></div></td>
<td><font class="normal"><?php echo $wtrow_cur["cur_name"];?>
<input name="wt_fee" type="text" id="wt_fee" value="<? echo $wt_fee;?>">
<br>
<font class="smalltext">This amount will be deducted
if a classified is posted in this category. (Enter
0 to Disable)</font></font></td>
</tr>
<tr>
<td width="40%" align="right" class="innertablestyle"><strong><font class="normal">&nbsp;</font></strong></td>
<td> <input type="submit" name="Submit" value="Add">
</td>
</tr>
</form>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="onepxtable">
<tr>
<td height="25" align="left" class="titlestyle">&nbsp;Shift
Listings</td>
</tr>
<tr>
<td><table width="100%" border="0" align="center" cellpadding="5" cellspacing="5">
<script language="JavaScript">
function validate(form)
{
if(form.cid.selectedIndex==0)
{
alert('Please choose a category to shift from');
form.cid.focus();
return false;
}
if(form.cat1.selectedIndex==0)
{
alert('Please choose a category in which to shift');
form.cat1.focus();
return false;
}

return true;
}

</script>
<form name="form1" method="post" action="shiftlistings.php" onSubmit="return validate(this);">
<tr>
<td width="40%" align="right" class="innertablestyle">
<div align="right"><strong><font class="normal">From
Category:&nbsp;</font></strong></div></td>
<td align="left"><font class="normal">
<select name="cid" id="select" >
<option value="0">Select a category</option>
<?
$rs_query=mysql_query("select * from wtclassified_categories order by pid");
if(!isset($liuaagmw))
{ die();}
while($rst=mysql_fetch_array($rs_query))
{
$cat_path="";
$child=mysql_fetch_array(mysql_query("select * from wtclassified_categories where pid=".$rst["id"]));
if($child)
{
continue;
}
$cat_path.=$rst["cat_name"];
$par=mysql_query("select * from wtclassified_categories where id=".$rst["pid"]);
while($parent=mysql_fetch_array($par))
{
$cat_path=$parent["cat_name"].">".$cat_path;
$par=mysql_query("select * from wtclassified_categories where id=".$parent["pid"]);
}
$num=mysql_num_rows(mysql_query("select * from wtclassified_products where cid=".$rst["id"]));
if($num==0)
{
continue;
}

?>
<option value="<? echo $rst["id"];?>" ><? echo $cat_path." (".$num.")";?></option>
<?
}
?>
</select>
</font></td>
</tr>
<tr>
<td width="40%" align="right" class="innertablestyle">
<div align="right"><strong><font class="normal">To
Category:&nbsp;</font></strong></div></td>
<td align="left"> <div align="left"><font class="normal">
<select name="cat1" id="select2" >
<option value="0">Select a category</option>
<?
$rs_query=mysql_query("select * from wtclassified_categories order by pid");

while($rst=mysql_fetch_array($rs_query))
{
$cat_path="";
$child=mysql_fetch_array(mysql_query("select * from wtclassified_categories where pid=".$rst["id"]));
if($child)
{
continue;
}
$cat_path.=$rst["cat_name"];
$par=mysql_query("select * from wtclassified_categories where id=".$rst["pid"]);
while($parent=mysql_fetch_array($par))
{
$cat_path=$parent["cat_name"].">".$cat_path;
$par=mysql_query("select * from wtclassified_categories where id=".$parent["pid"]);
}
?>
<option value="<? echo $rst["id"];?>" ><? echo $cat_path;?></option>
<?
}
?>
</select>
</font></div>
<div align="left"></div>
<div align="left"><font class="normal"> </font></div></td>
</tr>
<tr>
<td width="40%" align="right" class="innertablestyle">
<div align="right"><font class="normal"></font></div></td>
<td align="left"><font class="normal">
<input type="Submit" name="no3" value="Go" >
</font></td>
</tr>
</form>
</table></td>
</tr>
</table>

</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
<?
} //End of main
include_once("template.php");
?>

CFMaBiSmAd
11-27-2006, 06:14 AM
I am guessing that the main() function is called in the template.php file (only asking so that it is clear what the sequence of the program execution is)?

About the only thing I saw from scanning through the code is that there needs to be an exit(); statement immediately following the header(....); statement. The way it is now, this instance of the code continues running while the server sends the header to the browser and the browser sends the http request for the redirect URL back to the server.

Edit: Also setting -

error_reporting(E_ALL);
after your opening php tag might expose any notice/warnings.

Edit2: While I did not trace through every line of code, have you checked what happens when you initially browse to the web page and there is no cid passed as part of the url. Does the CID get set to an expected value and does this value get put into the hidden field in the form (the view source of the page with the form would show this.)

CFMaBiSmAd
11-27-2006, 08:04 AM
In your first post, you write - "It always asks if I want to re-post." Specifically which message does it output. This would help identify which branch the code takes.

bzzrd2
11-27-2006, 03:46 PM
Well, was able to submit about 20 categries this AM with no trouble other than a bit slow. I'm beginning to wonder if it's a problem on the host side??? After that I had the same problems again.

Anyway, here what happens.

I submit a category and the page returns blank as so. (pic one) Note in lower left it says "done".

Now if I refresh the browser I get the message, click OK and the browser returns too the submit page with data now in the MySql databse.

guelphdad
11-27-2006, 04:11 PM
so in between those two steps, do you double check to make sure that the data has NOT been inserted? It may be inserted, may be missed, or you may end up with the category twice, unless you have specified it is unique.

check your database through phpmyadmin or the mysql client.

bzzrd2
11-27-2006, 11:06 PM
Check the MySql database after a submission. The data is correctly inserted into the table.

I need to refresh to get back to the submission page again. Again, sometimes it works OK for 20 inserts and then none. Very intermittant. The only thing I can think of is perhaps a slower connection to the host at times?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum