PHP or Javascript

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

PHP or Javascript

Post by patch2112 »

Hello all, I'm not sure if this problem is being caused by PHP or Javascript, but...

I've got a PHP page that I am using to remove a product from the cart. I am passing an ID on the URL TO this page and retrieving with $_GET to identify the product to be axed.

This script simply deletes it from the database. I am then using a Javascript function (triggered by body onload) to return the use back the page they came from.

No problem, except, when the Javascript redirects the page, it's adding "?id=xxx" to the url, which I don't want it to do.

Any ideas? I've tried unset($id) but that didn't work.

Code: Select all

<?
//START SESSION
session_start();
header("Cache-control: private"); 


$id = $_GET&#1111;'id'];

//CONNECT TO DB

//DELETE THE PRODUCT
$sql = "delete from cart_contents where id="$id";
","$quantity","$color","$size")";

#execute the sql
$exc = mysql_query("$sql");
?>


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
function removed()
&#123;
window.location.pathname = "/cart_quantity_input.php";
&#125;
</script>

</head>

<body onLoad="removed()">
</body>
</html>
Thanks in advance,
Philip
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Post by patch2112 »

Oh yeah, as you can see I'm using sessions, which very well may be the problem, but I don't know much about them, just started using them.

Philip
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: PHP or Javascript

Post by timvw »

search for debugging 101 :p

error_reporting(E_ALL);
patch2112 wrote:

Code: Select all

$id = $_GET['id'];
might want to [php_man]mysql_escape_string[/php_man]

patch2112 wrote:

Code: Select all

//DELETE THE PRODUCT
$sql = "delete from cart_contents where id="$id";
","$quantity","$color","$size")";
$exc = mysql_query("$sql");
first see what you are doing and get eventual error messages:

Code: Select all

//DELETE THE PRODUCT
$sql = "delete from cart_contents where id="$id";
","$quantity","$color","$size")";
echo $sql;

$exc = mysql_query("$sql") or die(mysq_error());

patch2112 wrote:

Code: Select all

&lt;script type="text/javascript"&gt;
function removed()
&#123;
window.location.pathname = "/cart_quantity_input.php";
&#125;
&lt;/script&gt;
&lt;/head&gt;
&lt;body onLoad="removed()"&gt;
&lt;/body&gt;
&lt;/html&gt;
I wouldn't use JavaScript at all to do redirections..

Code: Select all

header('Location: http://foo/cart_quantity_input.php');
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Post by patch2112 »

Thanks Tim,

I don't think there is any errors. It's executing the sql and all script fine, but just adding the $_GET variable back on the end when I didn't tell it to. Also, why no Javascript to redirect? I'm requiring javascript because of the complexity of the ordering process, so that's okay, but are there other reasons not to? I've got redirects all over the place based on a navigation module in javascript, so that they only hit the ordering pages they need.

Thanks again,
Philip
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

actually i think your sql to delete a row is quite odd...

if i understand the query looks like:

Code: Select all

delete from cart_contents where id="5"; ","5", "blue", "xxl")
the reason why i suggest to not use javascript for redirection is that you don't know that users will have javascript enabled... while the header - location works like a charm ;)
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Post by patch2112 »

\",\"$quantity\",\"$color\",\"$size\")"; Yea This was junk in the sql that got taken erased after I posted, but still the same probelm. I think maybe it's a PHP setting that's appending the $_GET to the end?

I'm requiring Javascript for ordering so I'm okay there.

Any ideas on the URL being changed?
Post Reply