I'm tring to retrieve the numeric input and store it in the variabe q but I can't seem to access it using the method post this way.
<?php
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>€</h6>
<form action="" method="post">
<label name="qte">QTE:</label>
<input type="number" name="qte"><br><br>
<a href="panier.php?action=ajout&
l=<?php echo $row['nom_produit']?>&
q=<?php $_POST['qte']?>&
p=<?php echo $row['prix']?>"
onclick="window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=yes,
scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); return false;">
GOGOGO </a>
</form>
</div>
<?php
}
?>
</div>
I also tried with
<?php
if (isset($_POST['submit'])) {
$example = $_POST['qte'];
echo $example;
}
?>
There's a couple issues with this code. You were trying to reference qte in each loop through a $_POST variable, when what you really wanted was to just grab the input from the user. In an effort to clean up the code a little and make things more readable, I changed that big <a href... tag to trigger a function that will grab the qte value, form the URL and open the window. Note that I pass into the function the productID and prix values. I put them in quotes just in case they're blank. I also added an ID to your qte input element so we can get the value. Take a look and let me know if you need clarification
<?php
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>€</h6>
<form action="" method="post" onsubmit="return false;">
<label for="qte">QTE:</label>
<input type="number" name="qte" id="qte_<?php echo $row['ID_produit']?>"><br><br>
<a href='javascript:void(0)'
onclick='gogoLink("<?php echo $row['ID_produit']?>", "<?php echo $row['prix']?>")'>
GOGOGO </a>
</form>
</div>
<?php
}
?>
// then outside the loop is the function
<script>
function gogoLink(id, prix) {
let url ="panier.php?action=ajout"
url += "&l=" + id
url += "&q=" + document.getElementById("qte_"+id).value;
url += "&p=" + prix;
window.open(url, '', 'toolbar=no, location=no, directories=no, status=yes,
scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350');
}
</script>
Related
I have my data show with arrays, but I have a problem when I want to click on one of the data that appear all the same, must be different, what is wrong with my script
<?php
foreach($data as $row){
?>
<a href="javascript:void(0)" class="set_module">
<div class="col-lg-4 col-xs-6">
<div class="post-module">
<div class="tmb">
<img src="<?php echo base_url();?>assets/file/image-module/<?php echo $row->image; ?>"/>
</div>
<input type="text" name="id_m[<?php echo $row->id_module;?>]" id="id_m" value="<?php echo $row->id_module;?>"></input>
</div>
</div>
</a>
<?php } ?>
This my jquery
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $('#id_m').val();
alert(value);
return false;
});
the example above is I have data A, B, C and I show it by looping but when I click one of the data only data A keep appearing
Try like this
change id to class in this line
<input type="text" name="id_m[<?php echo $row->id_module;?>]" class="id_m" value="<?php echo $row->id_module;?>"></input>
and then in js
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $(this).find('.id_m').val();
alert(value);
return false;
});
fidle example
http://jsfiddle.net/Q8KVC/3543/
use css class insted of id. and find that class with $(this).
or use dynamic id like id="id_m<?php echo $row->id_module;?>" and change this <a href="javascript:void(0)" class="set_module"> with <a href="javascript:void(0)" class="set_module" data-uid="<?php echo $row->id_module;?>">
. after that change the script
as follows
$("a.set_module").on("click", function (event) { event.preventDefault(); var uid = $(this).data('uid'); var value = $('#id_m'+uid).val(); alert(value); return false;});
i have a dynamically created form that on click event shows/hides submit and cancel button. Its only returning the first form data. i have searched a lot and no luck. already tried unbind and each helpers. here is the code snippet.
<div class="wojo top right floated div">
<?php if (!$row->counter_offer): ?>
<a id="makeCounter" class="wojo positive label" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
<form method="post" id="<?php echo "formID" . $row->id; ?>" class="wojo_form" name="wojo_form">
<input class="wojo black label"type="text" placeholder="Counter Offer" name="offer">
<button type="button" data-action="processOffer" name="submitOffer" class="wojo positive label"
data-set='{ "action": "processOffer",
"id": <?php echo $row->id; ?>,
"current_offer" : "<?php echo $row->offer_price; ?>",
"sender_id" : <?php echo $row->sender_id; ?>
}' >Submit Counter</button>
<a id="<?php echo "cancelID" . $row->id; ?>" class="wojo negative label" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
</form>
<?php endif; ?>
<a id="rejectOffer <?php echo $row->id; ?>" class="rejectOffer" data-set='{
"parent": ".segment",
"id": <?php echo $row->id; ?>}' data-content="Remove Offer">
<span id="<?php echo "rejectID" . $row->id; ?>" class="wojo negative label"><?php echo 'Reject Offer'; ?></span></a>
</div>
//
$( document ).ready( function() {
$('.wojo_form').hide();
$('#makeCounter').each(function() {
$(this).unbind('click').click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
$('#cancelID' + $(this).data('id')).click( function() {
$('#formID' + $(this).data('id')).hide();
// $('[id = makeCounter] [data('id') = $(this).data('id')] ').show
$('#makeCounter').show
$('#rejectID' + $(this).data('id')).show();
});
});
});
});
</script>
it only works on the first counter offer button and the second counter offer doesn't do any thing. please help. I'm going nuts here for three days.
The "ID" attribute by definition cannot have duplicates! Since JQuery is aware of this, $('#makeCounter') will only select the first occurrence with that ID.
Also, there is no need to bind each event one by one, you can bind an event to an entire class of elements.
To fix the issue, use a classinstead of an id:
in the PHP:
<a class="wojo positive label makeCounter" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
You'll also want to set up your cancel button like this:
<a class="wojo negative label cancelButton" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
At this point, the each is unnecessary, you can bind all of them at once like this:
$(".makeCounter").click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
});
$('.cancelButton').click( function() {
$('#formID' + $(this).data('id')).hide();
$('#rejectID' + $(this).data('id')).show();
// check out the JQuery selector here:
$('.makeCounter[data-id="'+$(this).data('id')+'"]').show();
});
(you don't need the unbind either)
Ok guys already search for it but no results at all, and cant figured it out how to do this, so my problem is:
I have this form:
<form method="POST" action="search.php" name="form" id="form">
<input type="text" name="search" id="search" placeholder="Introduza a sua Localização">
<input type="submit" name="Submit" value="Pesquisar">
</form>
And what i would like to is when i press this:
<a onClick="">City</a>
The input value change to what i want in this case "City" and then automatically submit the form.
I already made a research about this and theres nothing i could take, any help would be appreciated.
Cumps.
Guys thanks to your help now its working but someone tell me please what the problem in this code:
<?php if (mysql_num_rows($tournaments) !=0){
do { ?>
<div id="mainContainer">
<div id="leftContainer"><img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png"></div>
<div id="rightContainer">
<div id="rightContent">
<p><?php echo $row_tournaments['description']; ?></p>
<div id="galleryButton"><p>Entrar no torneio</p></div>
</div>
<div id="rightDetails">
<i class="fa fa-gamepad"></i> <?php echo $row_tournaments['game']; ?><br>
<i class="fa fa-calendar-o"></i> <?php echo $row_tournaments['date']; ?><br>
<i class="fa fa-pencil-square-o"></i> Sem comentários<br>
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue()"><?php echo $row_tournaments['city']; ?></a><br>
<img src="images/<?php echo $row_tournaments['online']; ?>.png"> <?php echo $row_tournaments['online']; ?>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults"><p>Sem torneios</p></div>
<?php
}
?>
Everything is looped fine but that part:
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
Its not looping it only shows the first record why is it?
Any help would be appreciated.
in your js file you can check if the value of the textbox is equal to the cityname you want. if it is then submit the form. Attach the function to your a tag
function SubmitRightCity(){
if(document.getElementById('search').value == "citynamehere"){
document.forms["form"].submit();
}
}
in html
<a onClick="submitRightCity()">City</a>
In the case where you want your a tag to give the input a value, you attach a function to your tag, once the link is clicked, then the function will give your input a value.
<a onClick="giveThatInputAValue()"> City</a>
in javascript file you will have
<script>
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "My city value";
//if you want to submit the form right after, you add the line below
document.forms["form"].submit();
}
</script>
This is my a management exam that I'm trying to make a modification. The exam shows questions one by one hiding the other
When I press on one of the answers if the answer is correct, right before going to the next question the background
of the pressed question goes green if it was the correct answer, red if it was the wrong answer.
What I want: When the user presses the wrong answer I want the system not only to show a red background for the pressed button but
also make the background of the correct answer green. Showing this way to the user what was the correct answer. If I missed code please let me know.
PHP
$orderbyrand = "SELECT * FROM management
ORDER BY rand(), `answer` ASC LIMIT 2";
$rend_result=mysql_query($orderbyrand);
$k=1;
while($row_rand=mysql_fetch_array($rend_result)){
$answerarry[$k]= $row_rand['answer'];
$k++;}
$answerarry['0'] = $answerarry_shuffle['0'];
$answerarry['1'];
$answerarry['2'];
shuffle($answerarry);
if($answerarry_shuffle['0']==$answerarry[0])
{ $correcr_answer[0]=1; } else { $correcr_answer[0]=0; }
if($answerarry_shuffle['0']==$answerarry[1])
{ $correcr_answer[1]=1; } else { $correcr_answer[1]=0; }
if($answerarry_shuffle['0']==$answerarry[2])
{ $correcr_answer[2]=1; } else { $correcr_answer[2]=0; }
Javascript
function change_question(quest_num,optionvalue,ans_div_num){
var hidediv = 'question_'+optionvalue;
var add_optionvalue = (Number(optionvalue)+Number(1));
var showdiv = 'question_'+add_optionvalue;
var noofques = document.getElementById('noofques').value;
var totalno = (Number(noofques)+Number(1));
var totalcount = document.getElementById('noofques').value=totalno;
if(quest_num==1){
var correct_answer = document.getElementById('optvalue').value;
var optinoresult_add = (Number(correct_answer)+Number(1));
var addoptvalue = document.getElementById('optvalue').value=optinoresult_add;
document.getElementById('answerdiv_'+ans_div_num+'_'+optionvalue).style.background='#bbd387';
} else {
var correct_answer = document.getElementById('optvalue').value;
var optinoresult_add = (Number(correct_answer)+Number(0));
var addoptvalue = document.getElementById('optvalue').value=optinoresult_add;
if(ans_div_num!=0){
document.getElementById('answerdiv_'+ans_div_num+'_'+optionvalue).style.background='#dd5f5b'; }
}
}
HTML
<div class="row-fluid answer">
<div id="answerdiv_1_<?php echo $i; ?>" onClick="return change_question('<?php echo $correcr_answer[0]; ?>','<?php echo $i; ?>','1');" class="span12 show_cursor"><p><?php echo substr($answerarry[0],0,300);?></p></div>
</div>
<div class="row-fluid answer">
<div id="answerdiv_2_<?php echo $i; ?>" onClick="return change_question('<?php echo $correcr_answer[1]; ?>','<?php echo $i; ?>','2');" class="span12 show_cursor"><p><?php echo substr($answerarry[1],0,300);?></p> </div>
</div>
<div class="row-fluid answer">
<div id="answerdiv_3_<?php echo $i; ?>" onClick="return change_question('<?php echo $correcr_answer[2]; ?>','<?php echo $i; ?>','3');" class="span12 show_cursor"><p><?php echo substr($answerarry[2],0,300);?></p> </div>
</div>
I have a simple table that prints our the list of videos and plays them when you click on the row. I have a delete button to delete the corresponding video in the row. But for some reason when you click on any video it will only delete the last one in the list. I can look at the source on the page and the video_url seems to be correct. Any ideas?
Here is the majority of the code:
<?php
// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
echo $problem;
}
return $data;
}
if (isset($_POST['delete_video'])) {
$video_url = check_input($_POST['video_url']); //value here is always just the last row in the table!!!
echo $video_url;
$query_delete_video = "DELETE FROM `#__videos` WHERE `video_url`='$video_url'";
$db->setQuery($query_delete_video);
$db->query();
}
//list all details of the camera including camera_name
$query_videos = "SELECT #__videos.video_url, #__videos.video_size, #__videos.video_datetime, #__videos.video_length, #__cameras.camera_name
FROM #__videos INNER JOIN #__cameras using (user_id, camera_id)
WHERE #__videos.user_id=".$user->id." ORDER BY #__videos.video_datetime DESC";
$db->setQuery($query_videos);
//get number of cameras so we can build the table accordingly
$db->query();
$num_videos = $db->getNumRows();
// We can use array names with loadAssocList.
$result_videos = $db->loadAssocList();
echo "<html>";
echo "<head>";
?>
<link href="recordings/recordings.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="flowplayer/example/flowplayer-3.2.6.min.js"> </script>
<?php
echo "</head>";
echo "<body>";
?>
<?php
if (!isset($result_videos))
{
//TODO check if query failed
}
else
{
if ($num_videos == 0)
{
?>
<div id="cc_table">
<table id="webcam-table">
<thead>
<tr>
<th>Camera Name</th>
<th>Camera Details</th>
<th>Video Options</th>
</tr>
</thead>
<td colspan=3><b><i><center>You currently have no videos created. Start monitoring today!</center></i></b></td>
</table>
</div>
<?php
}
else
{
?>
<div id="cc_table">
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<table id="webcam-table">
<thead>
<tr>
<th>Camera Name</th>
<th>Camera Details</th>
<th>Video Options</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_videos;$i++)
{
?>
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Date Created: <?php echo $result_videos[$i]["video_datetime"]; ?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo $result_videos[$i]["video_length"]; ?> secs
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="return confirm('Are you sure you want to delete?')"/>
</td>
</tr>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
<?php
}
echo "</tbody>";
echo "</table>";
echo "</form>";
echo "</div>";
}
}
?>
<div id="player" style="display:block;width:320px;height:240px;background-image:url(recordings/landscape.jpg)"></div>
<script type="text/javascript">
function DoNav(theUrl)
{
//document.write(document.location.href = theUrl);
flowplayer("player", "flowplayer/flowplayer-3.2.7.swf", theUrl);
}
</script>
<?php
echo "</body>";
echo "</html>";
?>
Can you post the HTML? I suspect you've got multiple hidden input fields on the page, all with the same name. If that's the case, only the last one that loads on the page will register as a valid hidden input. A better way to do it would be to set a data attribute on the delete button, and then add a JS click event to that delete button. Something like this:
<button class="delete" data-video_url="youtube.com/abcd">Delete ABCD</button>
<button class="delete" data-video_url="vimeo.com/xyz">Delete XYZ</button>
and then when someone clicks on a .delete button, you send the data-video_url value to the PHP script via POST. A jQuery example might look like this:
$('.delete').click(function() {
var url = $(this).data('video_url');
$.post('/delete.php', { video_url: video_url }, function() {
// Do something on success
});
});
Another way to do it is to simply make each button its own form:
<form method="post" action="delete.php">
<input type="hidden" name="video_url" value="url_goes_here">
<button>Delete</button>
</form>
Hope that helps. Good luck.
The problem is that the name of the hidden field is not unique (for every iteration in the loop a new hidden field with the same name is created). How should the script know which row you want if all of them has the same name?
Your code does not seem to name the hidden values properly.
You should use $i to name the variable.
input type="hidden" name="video_url<?php echo $i; ?>"
then in handler you can do
<?php
if ($_POST) {
$kv = array();
foreach ($_POST as $key => $value) {
// check here which one begins with video_url
}
}
?>