.load() little part without toggle - php

I have a conversations flow who is refresh with .load() of jQuery every 3 seconds.
This system work, but in each conversation I have an answer button who slide a form (textarea and submit button) with .toggle().
To display the flow, I use a while with PHP.
My issue is, when the is loading by .load(), and answer button is clicked, the form hide again and text contain too.
<div id="the_river_loading">
<?php
$sql_the_river = 'SELECT u.nickname, u.firstname, u.lastname, u.main_photo, u.locality,
id_conversation, id_messages, owner, participants, text, date
FROM users AS u
INNER JOIN the_river
ON u.nickname = owner
WHERE answer = 0
ORDER BY date DESC';
$result_the_river = mysqli_query($mysqli, $sql_the_river);
$count = 0;
while ($data_the_river = mysqli_fetch_assoc($result_the_river))
{
$count++;
echo '<div class="message_container_news">'; // Start block conversation
echo '<a href="/profile/' . $nickname . '" class="name_links" style="vertical-align: top; font-size: 16px;">
<img src="' . $main_photo . '" title="' . $name . '" class="members_actu_photo" />' . $name . '</a>
<span style="vertical-align: top">, ' . $locality . '</span><span style="float: right; font-size: 12px;">Posté le Octobre 25, 2012</span>
<p style="margin-top: 5px;">' . $data_the_river['text'] . '</p>
<div class="btnAnswer_nb">' . $count_answer . '</div> Answers
See conversation
<div class="btnAnswer_news" id="btnAnswer_news_id_' . $count . '">Reply</div>
<form method="post" id="display_form_id_' . $count . '" action="" style="display: none;">
<br />
<textarea name="answer_text"></textarea><br />
<input type="submit" name="answer_valid_id_' . $count . '" value="Post" />
</form>
</div>'; // End block conversation
}
?>
</div>
<script type="text/javascript">
$(".btnAnswer_news").live("click", function(){
var num_show = this.id.replace(/\D/g, "");
$("#display_form_id_" + num_show).toggle("fast");
});
var auto_refresh = setInterval(
function() {
$("#the_river_loading").load("/home" + " .message_container_news");
}, 3000
);
</script>
In pleasure of read you.

This is because if you load the content again into the container, the styles applied per js will get removed (because they were applied through style="" and this gets refreshed). You should move the Form and the Button to another Div, wich is not in the loading Div.

If I understand your problem correctly (you've not be very descriptive), you just want to not hide the form when clicking again, for that use show() instead of toggle() , so replace your following line:
$("#display_form_id_" + num_show).toggle("fast");
for this one:
$("#display_form_id_" + num_show).show("fast");
If you want to keep the text of the textarea, you can like so:
var auto_refresh = setInterval(
function() {
tx = $("#the_river_loading textarea[name='answer_text']").val();
$("#the_river_loading").load("/home" + " .message_container_news", function(){
$("#the_river_loading textarea[name='answer_text']").val(tx);
});
}, 3000
);

Related

Link is cut after question mark in dynamic php table

I have a PHP dynamic table, and when I click on one of my link (http://www.xxxxxx.com/?r=xxxxxxxxx), the link open in a new tab and it's cut after the question mark (?), like www.xxxxxx.com/?.
Here is the table php code:
echo '<div class="row"><tr>';
echo '<td><form action='.$data['referral'].'>
<input class="btn" type="submit" formtarget="_blank" value='.$data['webadress'].' data-value='.gmdate("H:i:s",($data['timer']*60+60)).' data-start="false"></form></td>';
echo '<td>Now</td>';
echo '<td>'.$data['timer'].'</td>';
echo '<td>'.$data['payout'].'</td>';
echo '</tr></div>';
In my database, the $data['referral'] are my www.xxxxxx.com/?r=xxxxxxxxx link's.
I think is the type or something in my database option is wrong, so it cut after the question mark. Now I'm on varchar(255) and latin1_general_ci.
EDIT: I have put an input because I need store data for a timer, here is the JS:
$(window).load(function(){
var row = document.getElementsByClassName("color");
function toTimeString(seconds) {
return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
}
function redColor(element) {
$(element).css('background-color', 'rgba(255,0,0,0.7)');
}
function normalColor(elemen) {
$(element).css('background-color', 'transparent');
}
$('.btn').on('click', startTimer);
function startTimer() {
var dataStartElem = $(this);
var dataStart = dataStartElem.attr('data-start');
if (dataStart === 'false') {
dataStartElem.attr('data-start', 'true');
var nextElem = dataStartElem.parents('td').next();
var duration = dataStartElem.attr('data-value');
var a = duration.split(':');
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
setInterval(function () {
seconds--;
if (seconds >= 0) {
nextElem.html(toTimeString(seconds));
redColor(nextElem);
}
if (seconds === 0) {
nextElem.html('Now');
dataStartElem.attr('data-start', 'false');
clearInterval(seconds);
normalColor(nextElem);
}
}, 1000);
}
}
});
This JS, start the timer and put it in red, and after the "now" appear when is finish.
By using a GET form, you override the query part of the action. Either convert your form to a normal link, or put the query part of the URL in a hidden input.
As #Otto said, try to do it in way near to this:
echo '
<div class="row">
<tr>
<td>
' . htmlspecialchars($data['webadress']) . '
</td>
<td>Now</td>
<td>' . $data['timer'] . '</td>
<td>' . $data['payout'] . '</td>
</tr>
</div>
';
Here is the solution, thanks to all have help me to figured out: i think it's the form who dont take "?" on link, so i have changed it to a normal link and here we go.
echo '<div class="row">
<tr>
<td>
<a href="'.$data['referral'].'" target="_blank">
<input type="submit" value="'.htmlspecialchars($data['webadress']).'" data-value="'.gmdate("H:i:s", ($data['timer'] * 60 + 60)).'" data-start="false" class="btn" formtarget="_blank">
</a>
</td>
<td>Now</td>
<td>'.$data['timer'].'</td>
<td>' . $data['payout'] . '</td>
</tr>
</div>';

Change link text to icon

Hello I need change text to icon in my menu after hover on it.
I am getting data from db, so I need do it dynamically.
php in while
echo'<li style="width: ' . $row['item_width'] . 'px;">
<a href="stranky/' . $nazev . '/1/' . $row['id'] . '"
style="background-color: #' . $row['item_color'] . ';" alt="'. $row['item_name'] . '"
class="menu_data" id="search">'. $row['item_name'] . '</a>';
js
<script type="text/javascript">
$(document).ready(function(){
$(".menu_data" ).mouseover(function() {
var icon = $(this).attr('id');
$(this).html('<span class="glyphicon glyphicon-'+icon+'"></span>')
});
$(".menu_data" ).mouseout(function() {
var name = $(this).attr('alt');
$(this).html(name)
});
});
</script>
But it doesn't looks like nice. Have someon better idea?

Triggering function from jQueryMobile "slide panel", "list view"

In my jQueryMobile app I'm using slider for search part of application. When slider is opened and user writes "word to search" into search panel, list view of found results is printed out in format:
<li><a id='$id'>***search result***</a></li>
its loaded from php search file. On click of this li part of list view I need to trigger onClick function redirecting into another page and create variable. But this onClick trigger is not being picked.
This is pannel with search init:
<div data-role="panel" data-theme="b" id="mypanel" data-position="right" data-position-fixed="true" data-display="overlay">
<input type="search" name="search-mini" id="search-mini" value="" data-mini="true" />
<div id="search_results">
<ul data-role="listview" data-divider-theme="b" data-inset="true" id="sub_cont"></ul>
</div>
</div>
Php which sends search result:
if($uname == $check_uname){
echo "<li id='search_r'>" . "<a href='#'>" . $uname . " : " . " " . $fname . " " . $sname . "</a>" . "</li>";
}else{
echo "<li id='search_r'>" . "<a href='#' id='$id' class='s_result'>" . $uname . " : " . " " . $fname . " " . $sname . "</a>" . "</li>";
}
and jQuery:
$("#cc_page").live('pageshow', function(){
$("#search_r").click(function(){
var search_r = $('.s_result').attr('id');
window.location.href = "http://imes.jzpersonal.com/app/userpanel.html#sfpp_page";
});
});
but still click function is not being triggered. Anyone same experience? Anyone found a correct working way?
Solution:
$("#cc_page").ready(function(){
$("#search_r").live('click', function(){
search_r = $(this).attr('id');
window.location.href = "http://imes.jzpersonal.com/app/userpanel.html#sfpp_page";
});
});
This is not going to work. From what you have explained when you enter "word to search" listview is dynamically populated with li items, same li items that should have a click event on them.
Here's a problem. You are binding a click event in the wrong moment. In your case click event is bind at a pageshow event, and at this point listview is not populated with search results and because of how javascript works future li elements are not going to have a click event on them. Event can not be bind retroactively.
What you should do is to bind a click event only after elements have been appended to a listview.

jQuery UI picker with input box added using jQuery append

I am printing a form in a row on a page. The row contains a few columns which hold the form elements. Much like a table but in CSS.
When the user clicks on a link with the ID add_transaction_row then a little bit of jQuery is called which appends another row/div to the page. This row contains an identical set of div's and form elements as the row above.
I have named the elements date[], sub_cat_id[] etc so that I get an array of the updates to process on the next page.
I am using the jQuery datepicker for the date input box. I have a trigger on on class called .datepicker.
On the first row the jQuery date picker appears as expected. However when I click into any of the date input boxes on the rows below (the new rows/input box's added by jQuery append) then the datepicker does not work.
I was thinking that it might be something to do with me naming them all the same so I have given them all a unique ID. Still no luck.
I am now thinking its something to do with jQuery adding the .datepicker on page load and then when I click the bottom to add a new row this is adding the html to the source but after jQuery datepicker has loaded so it does not pick the new date/input box's up?
If anyone could shed any light on this I would appreciate it.
<?php
$my_new_split = '';
$my_new_split .= '<div class="split_transaction_box">';
$my_new_split .= '<div class="span-7">' . form_input('date[]', '', 'id="' . random_string('numeric', 8) . '" class="datepicker"') . '</div>';
$my_new_split .= '<div class="span-7">' . form_dropdown('sub_cat_id[]', $subcategories_options) . '</div>';
$my_new_split .= '<div class="span-5">' . form_input('reference[]') . '</div>';
$my_new_split .= '<div class="span-4 last">' . form_input('ammount[]') . '</div>';
$my_new_split .= '<div class="clearfix"></div></div><!-- /.split_transaction_box -->';
$my_new_split = trim($my_new_split);
$replace_this = array("\r\n", "\n", "\r");
$my_new_split = str_replace($replace_this, '', $my_new_split);
$my_new_split = str_replace('"', '\"', $my_new_split);
?>
<script>
$(document).ready(function() {
$('#add_transaction_row').click(function() {
$(".split_continer").append("<?=$my_new_split;?>");
return false;
});
});
</script>
<hr />
<div class="span-24 last">
<?=form_open('accounts/do_split')?>
<div class="split_continer">
<div class="split_transaction_box">
<div class="span-7"><?=form_input('date[]', '', 'id="' . random_string('numeric', 8) . '" class="datepicker"');?></div>
<div class="span-7"><?=form_dropdown('sub_cat_id[]', $subcategories_options);?></div>
<div class="span-5"><?=form_input('reference[]');?></div>
<div class="span-4 last"><?=form_input('ammount[]');?></div>
<div class="clearfix"></div>
</div><!-- /.split_transaction_box -->
</div><!-- /.split_container -->
<br />
Add new split
<input type="submit" class="button white" value="Save this split" />
<?=form_close()?>
Your exactly right, the datepicker does not work on new elements because you are adding those elements into the page after jquery has attached the datepicker. I've come across this issue myself. I came up with a few ways of doing it.
The first way was to bind a live click handler to the element, check if datepicker is applied, and if not apply it.
http://jsfiddle.net/uyx67/1/
But maybe a better way is to build the elements using jquery as objects and apply the datepicker that way
http://jsfiddle.net/4jmkw/3/
Both ways work, and do the job.
<script>
$(document).ready(function() {
$('#add_transaction_row').click(function() {
var new_row = jQuery("<?=$my_new_split;?>");
$(".split_continer").append(new_row);
$( ".datepicker" ).datepicker({
numberOfMonths: 2,
dateFormat: 'DD, d MM, yy'
});
return false;
});
});
</script>

Passing list item value during JQuery sortable() function

I'm trying to pass information from an unordered list, list item to an ajax request once the list item is placed in another list.
This is the JQuery that picks up the column #id the list item is dropped into, however the code for getting the $row['id'] picks up the first item in each list, not the specific item I'm sorting.
(I'm using php to iterate the list items in both lists - I've removed the unessential PHP code).
<script type="text/javascript">
$(document).ready(function(){
$(".sortable").sortable({
connectWith : ".sortable",
receive : function(){
var column = $(this).closest('div.box').attr('id');
var id = $(this).find('span').html();
$.ajax({
url: "update_column.php",
type:"POST",
data: "column="+column+"&id="+id
});
alert(column + id)
}
})
.disableSelection();
});
</script>
I'm using the alert() to give me some visual feedback. This is the HTML code:
<div id="one" class="box">
<ul class="sortable">
<li>
<div class="card">
<p>' . $row['customer'] . '</p>
<p>' . $row['ponumber'] . '</p>
<p class="hide"><b>ID:</b><span>' . $row['id'] . '</span></p>
<p class="hide">' . $row['misc'] . '</p>
</div>
</li>
</ul>
</div>
<div id="two" class="box">
<ul class="sortable">
<li>
<div class="card">
<p>' . $row['customer'] . '</p>
<p>' . $row['ponumber'] . '</p>
<p class="hide"><b>ID:</b><span id="id">' . $row['id'] . '</span></p>
<p class="hide">' . $row['misc'] . '</p>
</div>
</li>
</ul>
</div>
I'm a self taught programmer who has been programming for 3 months so I apologise for any novice mistakes.
I've updated your js, it works now. I have added comments where required.
$(document).ready(function(){
$(".sortable").sortable({
connectWith : ".sortable",
receive : function(event, ui){
//changed this to be use parent()
var column = $(this).parent().attr('id');
//get the current dragged item - as per http://jqueryui.com/demos/sortable/#event-receive
var index = ui.item.index() + 1 ;
//get the id from the span using the column we got in the first step and index we got above
var id = $("#"+column+" li:nth-child("+index+") span").html();
$.ajax({
url: "update_column.php",
type:"POST",
data: "column="+column+"&id="+id
});
}
}).disableSelection();
});

Categories