I am using the php code to empty my cart on my website if javascript is disabled. Only problem is that it empties the cart but submit the page too. I want it to remain on the same page and empty the cart because the page is supposed to submit for checking out not emptying the cart. Also I can't use javascript for this part because this is meant for user's who don't have javascript enabled.
I have tried using:
return false;
in the post function but it just made the page blank.
Here is my code:
<form method='post' action='checkout.php'>
// some input
<input type='submit' id='cart-empty' name='cartEmpty' value='Empty' />
</form>
if($_POST['cartEmpty']) {
$this->shippingfee = 0;
$this->empty_cart();
}
You mentioned in one of the comments that you weren't able to modify the headers (i.e. header('Location: form.php); because the header information has already been sent. To get around this, you should move your check for the posted cartEmpty value to the top of your submit page.
Headers are delivered any time content is echoed out to the browser (even white space such as blank lines), but if you do this processing prior to any output being sent, you have an opportunity to modify those headers still:
<?php
if($_POST['cartEmpty']) {
$this->shippingfee = 0;
$this->empty_cart();
header("Location: cartEmptied.php");
exit();
}
You can use this php code for validation in checkout.php and return user to page(if user doesn't fill the form):
if(empty($_POST['cartEmpty'])){
header("Location: ./form.php");
die;
}
Related
I have a form with a form-handler 'process-form.php. This form works as planned, however, whenever the form is submitted, before redirecting the user to another URL, (say, thank-you.html) it is showing the contents received from the form in process-form.php. How can I prevent that from happening?
if ($mail->send())
{
?>
<script type="text/javascript">
alert('Message Sent!');
window.location.replace("thank.html");
</script>
<?php
}
else
{
echo "<script type='text/javascript'>alert('ERROR!');</script>";
}
You mean shown in the URL, or is it in the HTML of the page? If it is the former, you can use
<form method="POST" ...>
If you mean the latter, you should change your PHP / JavaScript to not show the data
Hey so the issue you're seeing is that not all form fields are filled in so that you get an undefined variable error and you don't want users to see that.
Usually in a production environment (like my shared server) errors are hidden anyway just not on my localhost. To hide the errors though you can just use this:
error_reporting(0);
ini_set('display_errors', 0);
However, You'd be much better off making sure there was no error in the first place. To do this check if the fields were actually filled in otherwise set your variable equal to nothing so technically it will still exist even if it is blank.
if(isset($_POST['my_var'])){
$my_var = $_POST['my_var'];
}else{
$my_var = "";
}
You can also use the same if(isset()) logic where you are actually using the variables rather than where you set them
i am trying to redirect the page to previous page after edit the form..
i have these two url's for example
http://localhost/******/viewclientadmin.php?id=14
and
http://localhost/****/clientadminedit.php?id=106
so client after edit the form i want them to redirect to the viewclientadmin page
i already tried like this
if($DB->execute($chk_qry,$parms)) {
header("location:javascript://history.go(-1)");
}
but it's not going back..it staying on same page..
can you please help me out..how to do that..
thanks in advance...
Store your url in session like this and call that variable in another page in header
In your viewclientadmin.php page add these two lines
$page = "viewclientadmin.php?id=$id";
$_SESSION['page'] = $page;
And in your clientadminedit.php page add like this
if($DB->execute($chk_qry,$parms)) {
header("Location: " . $_SESSION['page']);
}
it will work...try
You can use
if($DB->execute($chk_qry,$parms)) {
header("Location: localhost***.php?$qry");
}
Instead of history.go by this way you will get your id on viewclientadmin and using that Id you will be able to fetch data from db and assign to your form.
I am trying to make the code for a shopping cart, which is working. However, when I refresh the page it's adding a product to my cart automatically.
For example, I have 3 products on my website, which are Apple, Banana, Orange.
I click on Apple and it's added to my cart with QTY 1 and the URL is showing
`mydomain.com/my-cart?action=addcart&p_id=FylvGt6Yyb6n%2BzTXcJHwjBawOY%2Fw3QSZxF7rdUJLqhA%3D#`
Now if I refresh the page then it's adding another Apple to my cart (QTY 2). Then if I refresh the page again it adds another Apple (QTY 3) and so on. I don't know why this is happing. It's adding to SESSION.
Would you help me in this?
Below Is my cart code.
$action = isset($_GET['action'])?$_GET['action']:"";
$p_id=$conn->real_escape_string($_GET['p_id']);
$decrypted_p_id = decryptIt($p_id);
//Add to cart
if($action=='addcart') {
//Finding the product by code
$query = "SELECT p_unique_id, p_images,p_name, p_currentprice FROM products WHERE p_id=?";
if ($stmt = $conn->prepare($query)) {
$stmt->bind_param("i", $decrypted_p_id);
$stmt->execute();
$stmt->bind_result($p_unique_id,$p_images, $p_name, $p_currentprice);
$stmt->fetch();
}
$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1; //Incrementing the product qty in cart
$_SESSION['products'][$decrypted_p_id] =array(
'qty'=>$currentQty,
'p_unique_id'=>$p_unique_id,
'p_images'=>$p_images,
'p_name'=>$p_name,
'p_currentprice'=>$p_currentprice
);
$product='';
// header("Location:cart.php");
}
Displaying product
<?php if(!empty($_SESSION['products'])):
foreach($_SESSION['products'] as $key=>$product):?>
/*some code here*/
endforeach;?>
<?php endif;?>
Edited code here Suggested by ADyson
if (isset($_POST['submit'])) {
$action=$conn->real_escape_string($_POST['action']);
$decrypted_p_id=$conn->real_escape_string($_POST['p_id']);
// whole code here
i found this code:
$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1;
this code always run when u reload the page,
give some condition if you want to add manually
If you simply click refresh on the exact same page then action=addcart etc. is still in the URL. Therefore inevitably it runs that action again when the page loads, and adds the items to the cart again.
An "add" action like that would be better done as a POST request, partly for semantic reasons (it's "sending" data rather than "get"ting it) and partly to avoid annoyances like this. Ideally a GET request should not cause any change of state in the application.
Instead of
Add to cart
you can do something like:
<form action="my-cart" method="POST">
<button type="submit">Add to cart</button>
<input type="hidden" name="action" value="addcart"/>
<input type="hidden" name="p_id" value="<?php echo $p_user_id;?>"/>
</form>
You can use some CSS to make the button appear more like your old hyperlink, if you wish.
Then, in your PHP cart code, simply replace all references to $_GET with $_POST instead, to read the values from the POST request.
This will have the advantage that the variables are not saved into the URL and therefore if someone tries to refresh the URL it will not automatically repeat the same action based on those variables.
You could also look into sending the data to add cart items via AJAX, so that it doesn't require a postback at all and usually results in a smoother user experience - if you look at most shopping websites now, that is generally what they do.
I don't know how to solve the variable $cat by following script.
"text" variable from Form to javascript and pass to php function to be $Categories_name and to be $cat.
I already test the $cat variable, it is not "String", it is "object", I don't understand.
But I need $cat to be "String".
when Test = "This is Cat" (3 words),
I test $cat by php str_word_count and the output is 1 (I need to correct answer 3);
I test $cat by php var_dump and no output (I need to correct answer "String").
<p id="CaTable"></p>
<script>
function CaFunction()
{
var text = document.getElementById("CategorySelect").value;
document.getElementById("CaTable").innerHTML = "<?php php_catable('" + text + "'); ?>";
}
</script>
<!-- Generate Table by php and MySQL-->
<?php
function php_catable($Categories_name)
{
$cat = $Categories_name;
.................
.................
$sql = "select * from table where xyz = '" .$cat. "'";
}
?>
You are confusing server side code and client side code. Your php code live on the server and can only be executed on the server. And your javascript is on your client's browser and does not know about the server (remember, php generate a text file, and only that text file is sent to the browser). if you want to use the php_catable() function from your client, you will need to do an AJAX call or to redesign your page to do a form submit (just like what Steve is proposing).
Your First Page:
Assuming CategorySelect is a dropdown select box, create a script for its onChange event and create a method="post"post form with a hidden input that goes to "generate_table.php".
<input type="hidden" name="ca_table" id="ca_table" />
You make ca_table a hidden input so php will pick up the value from it when this page gets submitted to a second page where you can generate your table using the php function.
<script language="javascript" type=text/javascript>
function CaFunction(){
documentGetElementById('ca_table').value = documentGetElementById('CategorySelect').value;
submit();
}
</script>
add this to your select dropdown:
onChange="CaFunction();"
Your Receiving Page:
So your receiving page "generate_table.php" would have
<?php
function php_catable($Categories_name)
{
$cat = $Categories_name;
.................
.................
$sql = "select * from table where xyz = '" .$cat. "'";
}
$category_name = $_POST['ca_table']; // cleaned up at least with suitable preg_replace etc
// and call your catable function
php_catable($category_name);
?>
So that way your result will have been posted back to the server as per comments about client side/server side by #Fluinc and answer by #litelite. To get it to do something which performs looking like innerHTML which changes a part of the page without submitting the whole page you will need AJAX, again as per #litelite's answer.
Might get marked down for being dependant on JavaScript but intended mostly to help clarify client v server.
If you want to avoid the JavaScript dependency of this script you could leave out the onChange altogether and add a submit button, then collect $_POST['CategorySelect']; assuming that is its name - ensure it has name="CategorySelect" for php as well as its Id for your css/javascript. Php gets its variable from the item's name.
To get something a bit like the effect of AJAX visually (though the page is still submitted) you could submit the page to itself using action="<?php echo $_SERVER['PHP_SELF']; ?>" on the form and have all the code on the one page. You can put the table generating code in the div where you want the table to appear - it would need a default state set, of course.
#litelite's comment regarding not using posted data directly in an sql query is also vital to prevent attack - make sure you clean it up before you use it!
I have two textareas, an iframe and a submit button. They are all in the same page. What I want to do is that; user will code some html and javascript codes in to the first textarea. the second one is already hidden to pass the javascript codes. After the user click submit button, s/he will see results in the iframe. actually what i am saying is something like w3school's trying page.
my submit button is:
<INPUT type="submit" value="Run the Codes" name="submit" onclick="sendthem()">
my textarea's are:
<TEXTAREA id="textareaCode1" style="WIDTH:100%;HEIGHT:400px" name="code22" rows="21" wrap="logical" cols="42"><?=$code;?></TEXTAREA> //the user's textarea
<textarea id="textareaCode" style="WIDTH:100%;HEIGHT:400px" name="code" rows="21" wrap="logical" cols="42"><?=$code;?></textarea> //this is actually hidden (style="display:none;") one but to see what happens, temporary i made it visible.
and my sendthem() function is:
function sendthem()
{
var t=document.getElementById("textareaCode1").value; //the visible textarea
t=t.replace(/</g,"SMALLER"); //string manipulation to pass js codes via php post
t=t.replace(/>/g,"GREATER"); //string manipulation to pass js codes via php post
t=t.replace(/=/g,"EQUAL"); //string manipulation to pass js codes via php post
document.getElementById("textareaCode").value=t; //manipulated code goes in to hidden textarea
document.getElementById("sampleform").submit(); //send the form
}
And my php code is:
<?
$code = $_POST['code'];
echo "$code"; // here i can see the manipulated code like "SMALLERhtmlGREATER"
?><br/><? // to try it :
$code = str_replace("GREATER", ">", $code); // I reverse the changes
$code = str_replace("EQUAL", "=", $code);
$code = str_replace("SMALLER", "<", $code);
printf("%s",$code); //and there is no js codes.. i tried echo or other php print commands also.
?>
Here is a screenshot http://i57.tinypic.com/sghcba.png (I hope it helps)
Now.. I think my problem is in the PHP part. Can anyone tell me why I can't see the JS part of code, after reversing changes ?
And please, I don't want to change all my codes with jQuery or AJAX etc.. I want to fix this code. Thanks in advance...