Here is my Html and PHP code, I've commented all the details, I want specific data depending on which button is clicked by the user, but the button is printed in a loop, whenever I try to access it gives the last updated data in submit button. because every time while loop executes, it is obvious to change values of $row variable, how can I manage to get appropriate values according to which submit button is clicked.
can it be done using an array?
any piece of code that can help?
<?php
require_once '../includes/header.php';
require_once '../includes/connection.php';
//i got data from table
$sql="select * from shoes";
$result = $con->query($sql);
?>
<html>
<main role="main">
<section class="sec-intro" role="section">
<h1>Products</h1>
</section>
<section class="sec-boxes" role="section">
<!-- I placed a PHP loop here -->
<?php
while ($row = $result->fetch_assoc()) {
?>
<article class="box">
<!-- Printing Data of Shoe -->
<img src="<?= $row['image'] ?>" width="250px" height="300px">
<h1> <?= $row['shoe_name'] ?></h1>
<?php
//cutting the long strings into short becasue shoe description was long
$des = $row['shoe_description'];
if (strlen($des)>70)
{
// truncate string
$stringCut = substr($des, 0, 90);
$endPoint = strrpos($stringCut, ' ');
$des = $endPoint? substr($stringCut, 0, $endPoint) : substr($stringCut, 0);
$des .= '... <a class= "link" href="search.php">Read More</a>';
}
?>
<p> <?= $des ?> </p>
<!------------------------------------------
----------------------------------------
---------- PROBLEM IS HERE -------------------
------------------------------------------
------------------------------------->
<input class="button" type="submit" name = "submit" value="Add to Cart" width= "150px"; />
<!-- THIS ADD TO CART BUTTON IS INSIDE LOOP
AND I WANT SHOE_ID AND SHOE_NAME OF THAT
SPECIFIC BUTTON OF SHOE SPECIFIC SHOE -->
</article>
<?php } ?>
<!-- PHP LOOP ENDED HERE -->
<?php
if(isset($_POST['submit']))
{
$id = $_POST['shoe_id']; //here i need the shoe id of that specific shoe.
}
?>
</section>
<!-- THIS WILL REMAIN SAME THROUGHOUT THE WEBSITE -->
</main>
<?php
require_once '../includes/footer.php';
?>
</html>
The image of the web page is attached for clarity:
page display
EDIT/UPDATE: The if(isset($_POST['submit'])) isn't working as well ??
If you want to redirect to another page how you have write in your comment then you have to use javascript. Change your submit button with:
<input class="button" type="button" onclick="location='your-next-page.php?id=<?=$row['shoe_name']?>'" value="Add to Cart" width= "150px"; />
And then you can retrieve the id by $_GET['id'];
If you want to post the id, then the you have to use a javascript function with ajax or fetch to send the id via post method.
Related
I want to make some kind of booking system. I am displaying all data about cars from the database with buttons(which is a link that directs to the booking page) in "boxes", each box is for one car. I want to get id of the car for which the user pressed the button in box where the car is.
Here is my code
<!-- Fetching data from rows -->
<?php
while($rows = $result->fetch_assoc())
{
?>
<div class="car_box">
<div class="car_title">
<p><b><?php echo $rows['Brand'],' ', $rows['Model'];?></b></p>
</div>
<div class="car_description">
<div class="car_details">
<!-- ENGINE CAPACITY -->
<p>
<b>Engine capacity: </b> <?php echo $rows['Engine_capacity']; ?> cm3
</p>
(...)
<!-- BOOKING -->
<div class="booking">
Book now
</div>
</div>
You could try adding a parameter to your link.
Book now
and retrieve it in your Bookings.php by using $_GET['id'].
I have this While loop in my php code where I echo rows inside one div that acts like a button and when I press that "button" I want to hide/show the connected div with the jQuery function "toggle". I only get it to work so that when I click any of the "buttons" it opens all divs and not just that one that's connected.
<script>
$(document).ready(function(){
$(".scorec").click(function(){
$(".scorematcho").slideToggle('slow');
});
});
</script>
That's the jQuery I'm using at the moment.
<?php $RM = mysql_query("SELECT * FROM score ORDER BY ScoreID DESC LIMIT 3");
while ($ScoreD = mysql_fetch_assoc($RM)) {
$a = $ScoreD["ScoreOne"]; $b = $ScoreD["ScoreTwo"];?>
<div class="scorec" >
<?php if($a>$b){;?><font color="green"><?php }else{?><font color="red"><?php }?>
<div class="scorel"><img src="flags/<?php echo $ScoreD["FlagOne"]; ?>.png" style="float:left;">
<?php echo $ScoreD["TeamOne"]; ?> | <?php echo $ScoreD["ScoreOne"]; ?></div>
<div class="scorem">-</div>
<div class="scorer"><?php echo $ScoreD["ScoreTwo"]; ?> | <?php echo $ScoreD["TeamTwo"]; ?>
<img src="flags/<?php echo $ScoreD["FlagTwo"]; ?>.png" style="float:right;"></div></font>
</div>
<div class="scorematcho" >
<div class="scorematch">
de_dust2
16-2
</div>
<div class="scorematch">
de_nuke
16-2
</div>
<div class="scorematch">
de_dust
16-2
</div>
</div>
<?PHP } session_destroy()?>
That's the HTML/PHP I'm using. The div "scorec" is the "button" and "scorematcho" is the div I wan't to toggle.The text inside "scorematcho" shall be PHP also, just haven't changed it yet. I have searched and tested some things but can't get anything to work properly, I have noticed that some put all their PHP code inside an "echo", why is that and could that be the problem? Hope that's enough information, please tell if not. Also, if you have some other improvements you think I should make to the code, please tell.
I have added this jfiddle Hope it helps!! http://jsfiddle.net/H77yf/
$(".scorec").click(function(){
$(this).next().slideToggle('slow');
});
I have some toruble with displaying terms of service.
At the cart page all works fine: http://mtxt.ibroken.ru/component/virtuemart/cart.html?Itemid=0
(bottom link) opens popup with text, generated by
<?php echo $this->cart->vendor->vendor_terms_of_service; ?>
code.
But i have button on the shop page http://mtxt.ibroken.ru/magazin.html (top button at right side), which must display same text...
At present moment text written in /modules/mod_virtuemart_cart/tmpl/default.php file. But how to get it in this file from shop interface by using PHP?
pps. Ugly English, sorry for that :)
You need to modify /modules/mod_virtuemart_cart/tmpl/default.php (or your override) and add this code just after line 3:
vmJsApi::js ('facebox');
vmJsApi::css ('facebox');
$document = JFactory::getDocument ();
$document->addScriptDeclaration ("
jQuery(document).ready(function($) {
$('div#full-tos').hide();
$('a#terms-of-service').click(function(event) {
event.preventDefault();
$.facebox( { div: '#full-tos' }, 'my-groovy-style');
});
});
");
And add this code just after line 53
<div class="show_cart">
<?php
if(!class_exists('VirtueMartModelVendor'))
require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'vendor.php');
$vendor = VmModel::getModel('vendor');
$vendor = $vendor->getVendor();
?>
<br />
<span style="z-index: 0;">
<a href="<?php JRoute::_ ('index.php?option=com_virtuemart&view=vendor&layout=tos&virtuemart_vendor_id=1') ?>" class="terms-of-service" id="terms-of-service" rel="facebox" target="_blank">
<?php echo JText::_ ('COM_VIRTUEMART_CART_TOS_READ_AND_ACCEPTED'); ?>
</a>
</span>
<div id="full-tos">
<h2><?php echo JText::_ ('COM_VIRTUEMART_CART_TOS'); ?></h2>
<?php echo $vendor->vendor_terms_of_service; ?>
</div>
</div>
That shoud do the trick!
OK, I've been struggling with trying to pass parameters from the page to the popup div (within the same page) using jquery mobile. I'd just like to be able to edit or items from the database within a popup.
Here's the array of items I'm trying to update:
<div class="ui-block-c">
<?php
$fam_mem = $_db->rows('SELECT * from family_member', array());
foreach($fam_mem as $f)
{
echo "<a href='?id=$f[member_ID]#edit_fam_mem' data-mini='true' data-theme='d' data-inline='true' data-role='button' data-rel='popup' data-position-to='window' data-transition='pop' data-icon='gear' data-iconpos='notext'>Edit</a>";
echo "<a href='?id=$f[member_ID]#delete_fam_mem' data-mini='true' data-theme='d' data-inline='true' data-role='button' data-rel='popup' data-position-to='window' data-transition='pop' data-icon='delete' data-iconpos='notext'>Delete</a>";
}
?>
</div> <!--End ui-block-c-->
Here's the code for the popup div:
<div data-role="popup" id="delete_fam_mem" data-theme="d" data-overlay-theme="b" class="ui-content" style="max-width:340px;">
<?php //include_once("./delete_fam_mem.php");?>
<h3>Delete Family Member</h3>
<?php
echo "id is ", $_GET[id]; exit;
$del = $_db->query("DELETE FROM family_member WHERE member_ID = ", array($_GET[id]));
if ( $del )
echo "Chore $_GET[title] deleted successfully.";
else "Could not be deleted, maybe you should go to counseling.";
?>
<button class="button" onclick='closefancy()'>OK!</button>
</div> <!--End Delete Family Member-->
Some reason it will not pass the id from the page to popup. Any help would be greatly appreciated! Thanks!
just submitted a proposal which suggests how one could take parameters from the opener-button into the popups beforeposition event, see https://github.com/jquery/jquery-mobile/issues/7136
Put in your tag id property. Like 'id'.
And put one element to recive the value in your page. Like:
<input type="hidden" name="recivedId" />
So you can do something like that using jquery:
$('#a').on('click',function(){
$('#recivedId').val($(this).attr('href'));
})
This is my first serious foray into javascript/ajax.
Overview:
I have an index page listing multiple records, each on different rows. For each record I have a link which pops open a small javascript window which contains a symfony form. All goes well, with the exception that I can't figure out how to pass the popup javascript the object Id of each record. Here's what I have:
Starting with the Action:
public function executeTrackReferrals(sfWebRequest $request){
$userId = $this->getUser()->getId();
$this->pager = new sfDoctrinePager('referral', sfConfig::get('app_pager'));
$this->pager->setQuery(Doctrine_Core::getTable('Referral_submissions')->getUsersSubmissions($userId));
$this->pager->setPage($request->getParameter('page', 1));
$this->pager->init();}
Main index page:
<?php
include_partial('<a bunch of other includes >');
include_partial('referral/rtsIndex', array('pager' => $pager));
?>
_rtsIndex partial:
<table>
<?php foreach ($pager->getResults() as $r => $referral): ?>
<?php
$referralObject = Doctrine_Core::getTable('referral')->getReferralObjectById($referral->getId());
$submissionObject = Doctrine_Core::getTable('referral_submissions')->getObjectByReferralId($referralObject->getId());
?>
<TR VALIGN=TOP>
<td WIDTH=10% ALIGN="center" class="_7">
<P ALIGN=CENTER>
<script type='text/javascript'>
$('.popup_changestatus').click(function(){
// put the row id into the hidden field in the popup
var rowId = $(this).parent().find('span.row_id').html();
$('test').val(rowId);
fg_popup_form("fg_formContainer","fg_form_InnerContainer","fg_backgroundpopup");
return false;
});
</script>
<a href="#" class="popup_changestatus">
<?php echo utilities::getStatusCode($submissionObject->getCandidateStatus()); ?>
</a>
<span class="row_id" style="display: none">
<?php echo $referral->getId() ?>
</span>
</P>
</td>
</TR>
</table>
the Javascript popup form code mentioned above, includes this line:
<?php include_partial('referral/changeStatusCodeForm'); ?>
The above line renders the actual symfony form:
_changeStatusCodeForm partial:
<?php
$object = new referral_submissionsForm(<this is where I need to pass an ID for each popup form>);
echo $object;
?>
Can anyone steer me in the right direction?
If interested in the actual javascript code, it's a pretty nifty open source popup:
http://www.html-form-guide.com/contact-form/simple-modal-popup-contact-form.html
EDIT:
Here is the contents of contactform-code.php:
<script type='text/javascript' src='/project/misc/simple-popup-form-1/scripts/gen_validatorv31.js'></script>
<script type='text/javascript' src='/project/misc/simple-popup-form-1/scripts/fg_ajax.js'></script>
<script type='text/javascript' src='/project/misc/simple-popup-form-1/scripts/fg_moveable_popup.js'></script>
<script type='text/javascript' src='/project/misc/simple-popup-form-1/scripts/fg_form_submitter.js'></script>
<div id='fg_formContainer'>
<div id="fg_container_header">
<div id="fg_box_Title">Change Status</div>
<div id="fg_box_Close">Close(X)</div>
</div>
<div id="fg_form_InnerContainer">
<form id='contactus' action='javascript:fg_submit_form()' method='post' accept-charset='UTF-8'>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
<input type='text' class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />
<div class='short_explanation'>* required fields</div>
<div id='fg_server_errors' class='error'></div>
<div class='container'>
<?php
// $form = new referral_submissionsForm();
include_partial('referral/changeStatusCodeForm');
?>
</form>
</div>
</div>
First, you shouldn't directly use Doctrine_Core into your model. The best way here, is to make a leftJoin on your 2 tables when you pass the query to the pager. Then, in your template, you just fetch the relation (like $object->getRelation()).
Regarding to your problem, you should pass the ID when building the <div id='fg_formContainer'> since it's the content of your popup. The part come from this file: contactform-code.php.
Show us where do you build this div and you will have the place where you have to pass the id.
Edit:
Well, I finally get your problem. You have one popup define on your page that can be called on each click from each row. And you want to know how to pass the row id to the global popup.
This can be done in javascript. Instead of :
<a href='javascript:fg_popup_form("fg_formContainer","fg_form_InnerContainer","fg_backgroundpopup" );'>
<?php echo utilities::getStatusCode($submissionObject->getCandidateStatus()); ?>
</a>
You can better use :
<a href="#" class="popup_contact">
<?php echo utilities::getStatusCode($submissionObject->getCandidateStatus()); ?>
</a>
<!-- use this hidden span to put the row id, to be able to retrieve it using jQuery -->
<span class="row_id" style="display: none">
<?php echo $referral->getId() ?>
</span>
And then, define an action using jQuery (for example):
$('.popup_contact').click(function() {
// put the row id into the hidden field in the popup
var rowId = $(this).parent().find('span.row_id').html();
$('#submitted').val(rowId);
fg_popup_form("fg_formContainer", "fg_form_InnerContainer", "fg_backgroundpopup");
return false;
})