I am currently doing a project, and I want to know if it's possible if I could click an image then it will send a query to the database. since I am doing a project about Online menu. where I will click the image then there'll be query to be sent in the database, and after that the customers order wil appear in the side of the screen. is this possible? if so how ?
You really have several options of how you want to do this. The most basic being to surround your image in a link tag like so:
<image />
You can then either handle the click with an actual refresh. Or, you can handle it with ajax if you don't want the page to refresh.
Do your work on another php page.
If you need to do this through ajax. Let's suppose you want to load the content sent from the server in the div 'itemdetails'. You can specify this as the image tag(using jquery:
<img src="url/of/image" onclick="$('#itemdetails').load('http://yoursiteurl/items?id=itemid')">
The best way of doing that for me is using jquery like this:
Javascript:
function orderedSomething(id) {
$.post("ajaxOrder.php",{ orderID:id,rand:Math.random() } ,function(response) {
if(response=='ok') {
$("#alert").fadeTo(250,0.1,function() {
$(this).html('Your order is coming.').fadeTo(150,1);
});
} else {
$("#alert").fadeTo(250,0.1,function() {
$(this).html(response).fadeTo(150,1);
});
}
});
}
The php file ajaxOrder:
<?php
if($_POST['orderID']) {
$res = $query('check stock'); //Assuming that there is no info related to your table
if(mysql_num_rows($res)>0) {
$query('insert DB'); //Assuming that there is no info related to your table
echo 'ok';
} else {
echo 'Your order is out of stock';
}
}
?>
So your html should be:
<div id="alert"></div>
<img src="images/ice-cream.gif" onClick="orderedSomething(<?=$iceId?>)" style="cursor:pointer" />
Related
I got a login system, and what I want to do is to hide a div and show another div when the user types the incorrect login details. However it doesn't seem to work.
if(...) { // here I check if the user enters the correct information
... //much code inside
} else { // if incorrect info, do this
echo "<script>$('#jqueryhide2').hide();
$('#jqueryhide').show();</script>";
}
Tried to google a bit but can't find anything that could solve my problem.
put this code and also include your Jquery file -(dont forget)
echo "<script>
$('document').ready(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
I think you should missing the Onclick function like this:
<script>
$('loginId').Onclick(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
You don't need the script tags inside your statement. You can simply do the following. ( Make sure jqueryhide and jqueryhide2 are a part of the DOM. )
if (blah == blah )
{
$('#jqueryhide2').show();
$('#jqueryhide').hide();
}
else
{
$('#jqueryhide2').hide();
$('#jqueryhide').show();
}
I have a link in a php while loop
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
The pop up requires the product id to search the database but hash tag is client side. I tried to use javascript window.location.hash but the outcome was not very reliable.
Does anyone know a method preferably server side I could use to retain the active product id while I call the pop up, attain the product id, use it to query the database and output it in the pop up.
I have a session already started and tied to a different condition.
I tried to call the product id directly from the pop up but because of the loop I only get either the first or last in the array.
<?
while ($i < $num) {
$product_id=mysql_result($result,$i,"prod_id");
$title=mysql_result($result,$i,"lTitle");
//main page
echo "<b>" , $title;
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
?>
<!------pop up--------->
<script type="text/javascript">
function pop_up(){
document.getElementById('pop').style.display='block';
}
</script>
<div id="pop">
<p style='color:#6F0A0A; font-size:15px;'><? echo $product_id; ?></p>
</div>
<?
$i++;
}
?>
I'll try answering but to be honest the question is very vague and the code is a bit messy.
First off, you can simply send the product_id as a GET variable to the new popup and read it in PHP. Something like this will work:
echo "<a href = 'http://www.mydomain.com/popup.php?product_id=$product_id' onclick="window.open(this.href, 'popup_win',
'left=100,top=100,width=500,height=500,toolbar=1,resizable=0'); return false;" id = 'linker' >See more</a>";
On your popup.php file (the popup page) you will get the product_id with the PHP $_GET method:
$product_id = $_GET['product_id'];
and then do whatever MySQL query you want, now that you know $product_id.
I hope this helps, if that's not exactly what you meant please add more details so I can revise my answer.
Well, you could first load all this records first and place them into the popup content or, make an ajax request, open the popup, and when the request is done successfully place the values returned into the popup content. Better with JQuery
Basically I'v got a HTML Form that links to a php file in a different location for it's action, Currently I'm using the form to update the users profiles and then send them back to the editprofile.php. Basically at the top of editprofile.php if they've submitted the query I want to display the result of either "Profile Updated" or "Failed to Update", issue is I can't workout how to display query results when the query is in a different file.
I tried to do this;
<?php
if(!$query)
{
echo '<div class="editfail">Profile failed to update!</div>';
}
else
{
echo '<div class="editsuccess">Profile successfully updated!</div>';
}
?>
Except the issue with this is that the query hasn't been run on this page, it was run from another page and then redirected back to the editprofile page using a header, so how can I display the same results as above when the query is being executed from another location?
You can send parameter when you are redirecting back the file.
example
if(mysql_query($update_query))
{
header('location:editprofile.php?msg="success to save"');
}
else
{
header('location:editprofile.php?msg="failed to save"');
}
Or even you can send flag also
if(mysql_query($update_query))
{
header('location:editprofile.php?flag=0');
}else
{
header('location:editprofile.php?flag=1');
}
And check the value of flag in your editprofile.php file to display proper message.
You shouldn't mess around with the headers fxn unless you need to - depending on output_buffer settings etc they can be a pain:
You can do what you want - all in 1 single page:
So something like this -As a matter of common convention, and to a degree security, you should post the form to itself - you can integrate whatever else from the other page into the pass/fail profile logic block:
<?php
$query = htmlentities($_POST['profiletext']); #sanitize avec tu code du jour
if(!$query || $query != 'someacceptablevalue))
{
#If it's not posted, or its not a good value, tell them it failed
# and redisplay the form to try again
$query_msg = '<div class="editfail">Profile failed to update!</div>';
$profile_form = "<div_class='profile_rest_of_page stuff'>
<form action='#' method='post'>
<input type='text' id='profiletext' name='profiletext/>
</form>
</div>";
}
else
{
# They did it - Success, and link to next step
$query_msg = '<div class="editsuccess">Profile successfully updated!</div>';
$profile_form = 'No form needed - you did it';
}
#One block below handles all in 1 page with above logic:
echo "<body>
<div class='profile_message_container'>
$query_msg
</div>
<div_class='profile_rest_of_page stuff'>
$profile_redo<br/> You did it <a href='next'>next</a>
</div>
</body>
";
?>
You can do this in two ways:
Send the query results in the link like a GET which could be tampered with
Process the form in the same page that has your form as follows
if(isset($_POST['some_name'])) {
// Process form
} else {
// Display form
}
I have an active session in my page using: $_session_start(); I want to hide part of my form and show another based on the users previous entries.
Basically I offer Direct Deposit and Paper Check I want to be able to have the user only see the fields required for them to complete previously (in signup so the values are already in the database)
Right now the database table is set up like this:
paper_check with a value of 1 (yes pay this way) or 0 (no pay this way)
and the same thing with direct deposit. I need a way to show/hide the fields associated with each based on the users previous selections.
I typed this up but it doesn't seem to do anything. Please help me!!!!! (The class names hide and show are in my css and working properly!)
<?php
if ($_SESSION['direct_deposit'] == 1)
{
$doc->getElementById('check_payable_to')->className+" hide";
}
else
{
$doc->getElementById('name_on_account')->className+" hide";
$doc->getElementById('check_payable_to')->className+" show";
}
?>
I don't see a className property in the PHP DOM library. It looks like this is the way to add a class to an element:
$doc->getElementById('check_payable_to')->setAttribute('class',
$doc->getElementById('check_payable_to')->getAttribute('class') . " hide");
Do you have error reporting enabled? It should be warning about the unknown property.
If you want to do this in Javascript instead of PHP, have your PHP do something like:
<script>
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1) {
/* Do what you want */
} else {
/* Do what else you want */
}
</script>
In your question you said "I have an active session in my page using: $_session_start();" You shouldn't use $_session_start(). It should be session_start() .
className+" hide" should be className .= " hide"
By wrapping each section in it's own div like so:
<div id="paperCheck class="show">
<!--Your Paper Check Information here-->
</div>
<div id="directDeposit" class="show">
<!--Your Direct Deposit Information here-->
</div>
And then by using javascript you are able to do the following:
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1)
{
document.getElementById('paperCheck').className ="hide";
}
else
{
document.getElementById('directDeposit').className ="hide";
}
I want to enable or disable a div according to the session if it starts with an user or a guest. I want to do something like this:
First, i will evaluate if it is user or not by doing this:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
?>
then in jquery, i would like to say:
$('.box').click(function(){ // labBox appears when box is clicked
if(<?php $guest?>)
$("#LabBox").hide();
else
$("#LabBox").show();
});
Question: how can i use my php boolean var $guest to disable or hide some elements of my website?
Do i have to do two distinct php files? one for users and other for guest (e.g, home.php and home_guest.php)?
you could do the alternative such as
<script>
var guest = '<?php echo $guest; ?>';
$('.box').click(function(){ // labBox appears when box is clicked
if(guest === "true") {
$("#LabBox").hide();
} else {
$("#LabBox").show();
}
});
</script>
This would simply allow you to pass the PHP value to a Javascript variable, in order for you to use it within the onClick.
Remember: everything that reaches the client can be manipulated. Therefore, if you send an hidden element (say, an hidden <div>) any tech-savvy user can, and will, easily make them visible.
You MUST perform the checks about the login/guest status in your PHP script, and don't rely on jQuery to assemble the page at client side (hey, after all, the user may have disabled javascript altogether!)
You don't need two pages (eg: home.php and home_guest.php) to render different content based on the user level. Just use appropriately session/cookies and different echos.
Use a hidden input, populated by PHP, which jQuery can grab:
<?php
echo "<input type=hidden id=guestcheck value=$guest/>"
?>
if ("#guestcheck").val()) {
}
I personally like this method because it allows me to check the source when debugging to find out where any errors may be (for instance you can plainly see in the source when viewing the page whether or not GUEST is true)
It depends on contents of those files. If the only difference is visibility of the block, it's more reasonable to do the check inline.
<?php if (isset($_SESSION['idUser'])) { ?>
$('.box').click(function() { $("#LabBox").show(); }
<?php } ?>
Personally I would do it in the HTML rather than the JS file...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$loggedin=true;
} else {
$loggedin=false;
}
?>
Then later on..
<?php if($loggedin===true){?>
<div>User is logged in</div>
<?php }else{?>
<div>Guest is viewing page</div>
<?php }?>
This means that the div for the user is not shown to the guest, whereas your currently solution only hides it from view (user could just use firebug/viewsource!
Why don't you just show/hide your div in the php depended on if they are a guest or not...
So...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
if($guest===true){
echo "<div></div>";
}
else{
//dont echo div
}
?>
PHP / server-side:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
// add #LabBox element from here to avoid junk/hidden elements for guests
}
?>
JQuery / client-side:
$('.box').click(function(){ // labBox appears when box is clicked
if (!<?php echo $guest?> && $('#LabBox').length > 0) {
$('#LabBox').show();
}
});
Then it is critical that any action requested by the user pass the "guest or not?" test before being granted from the server-side.