Hide/show rows by php+jQuery - slidetoggle

I'm very new in PHP and JS. I have table that form by PHP. I want to hide rows that formed by PHP.
<script>
$(document).ready(function() {
$("#"+"row-"+"<?php echo $line->id; ?>").click(function(){
$("#"+"<?php echo $line->showOptionals($extrafields, 'view', array('style'=>'class="drag drop oddeven"', 'colspan'=>$coldisplay), '', '', 1); ?>").slideToggle("slow");
});
});
</script>
For each row that formed by $line->id, have each $line->showOptionals...
Is it possible? Because site crashed...
Red row formed with $line->id, green rows formed with $line->showOptionals. If add more red row, list become very long. So i want to hide each green rows
Server have ERR 500
mod_fcgid: stderr: PHP Parse error: syntax error, unexpected 'view' (T_STRING) in htdocs/main.inc.php on line 1528, referer: /comm/index.php?mainmenu=commercial&leftmenu=
line 1528:
$("#"+"<?php echo print $line->showOptionals($extrafields, 'view', array('style'=>'class="drag drop oddeven"', 'colspan'=>$coldisplay), '', '', 1); ?>").slideToggle("slow");

Sorry...my fault. The code was open by PHP, so one line was open by ' and close on '...i missed it. Thank you.

Related

Add the product to cart using WWW::Mechanize - Perl

I'm writing a script that selects a size and adds the product to cart here is where it is
http://store.nike.com/us/en_us/pd/free-4-flyknit-running-shoe/pid-1064825/pgid-1481072
use WWW::Mechanize::Firefox;
$mech = WWW::Mechanize::Firefox->new();
my $tconike = "http://store.nike.com/us/en_us/pd/free-4-flyknit-running-shoe/pid-1064825/pgid-1481072";
$mech->get($tconike);
print $mech->uri();
$mech->submit_form(
form_number=> 2,
fields => {
skuAndSize => $shoesize,
click => "ADD TO CART",
}
);
But here is the output
Uncaught exception from user code:
No form found to submit. at nikecartstandalone.pl line 25
at C:/Users/Brett/Documents/brett/Perl/perl/site/lib/WWW/Mechanize/Firefox.pm l
ine 2162
WWW::Mechanize::Firefox::signal_condition('WWW::Mechanize::Firefox=HASH(
0x2a54888)', 'No form found to submit.') called at C:/Users/Brett/Documents/bret
t/Perl/perl/site/lib/WWW/Mechanize/Firefox.pm line 3649
WWW::Mechanize::Firefox::submit_form('WWW::Mechanize::Firefox=HASH(0x2a5
4888)', 'form_number', 2, 'fields', 'HASH(0x3501328)') called at nikecartstandal
one.pl line 25
Anyone know what I did wrong, is it because I should have used something besisdes submit_form or is it something else?
As the error says it's not able to find the form, so try submitting form with with_fields where you can specify the fields which are there in the form you are going to submit, that'll be easier to search and submit.
Eg: $mech->form_with_fields('username');
will select the form that contain a field named username.

Zend Form TextArea with countdown characters and StringLength Validator - different calc

In my form i have a zend_form_element_textarea. For this element i add a validator StringLength like this:
$this->addElement('textarea', 'text', array('label' => 'F_MESSAGE_SMS_TEXT', 'required' => true, 'class' => 'fieldLiveLimit', 'limit' => 160));
$this->text->addValidator('StringLength', false, array('max' => 160));
and i use a script javascript for show live countdown characters:
//Text field or text area limit - get limit by field parameter
$(".fieldLiveLimit").each(function () {
var characters = $(this).attr('limit');
var remaining = calcDifference(characters, $(this).val());
if ($('.limitCounter').length > 0) {
$(this).after($('.limitCounter').first().clone());
$(this).next('.limitCounter').children('span').html(remaining);
} else {
$(this).after($("<div class='limitCounter'>" + translate('L_LIMIT_COUNTER', [remaining]) + "</div>"));
}
checkClassCounter(remaining, $(this));
$(this).bind('textchange', function (event, previousText) {
remaining = calcDifference(characters, $(this).val());
checkClassCounter(remaining, $(this));
if ($(this).val().length > characters) {
$(this).val($(this).val().substr(0, characters));
} else {
$(this).next('.limitCounter').children('span').html(remaining);
}
});
function calcDifference(characters, value) {
return characters - parseInt(value.length);
}
function checkClassCounter(remaining, element) {
if (parseInt(element.val().length) == 0) {
element.next(".limitCounter").hide();
} else {
element.next(".limitCounter").show();
if (remaining <= 10) {
element.next(".limitCounter").addClass('red-message');
} else {
element.next(".limitCounter").removeClass('red-message');
}
}
}
});
this works well, except for one thing. If inside the text area there are the new lines, the validator zend the new line it counts as two characters, while my JS script as one.
who is wrong? I think the zend validator, but it seems really strange as a thing and then ask to you!
It has to do with the line breaks, as user Pankrates already pointed out in his comment.
In fact, this problem is a lot more complex than it seems at first, because it has at least two dimensions:
jQuery strips the carriage return character in the val() function: "At present, using .val() on textarea elements strips carriage return characters from the browser-reported value." jQuery documentation.
It is inconsistent along browsers how line breaks with \r\n are counted. See here or here for related questions on SO. However, on all browsers I have installed on my system (Firefox 20.0 and Chrome 26.0), \r\n are counted as two characters, so I cannot confirm this.
See this little code snippet for a demonstration:
<?php
$str1 = "test\nstring";
$str2 = "test\r\nstring";
?>
<textarea id="text1"><?php echo $str1 ?></textarea>jQuery: <span id="jquery1"></span>, JS: <span id="js1"></span>, PHP: <?php echo iconv_strlen($str1) ?>
<textarea id="text2"><?php echo $str2 ?></textarea>jQuery: <span id="jquery2"></span>, JS: <span id="js2"></span>, PHP: <?php echo iconv_strlen($str2) ?>
<script type="text/javascript">
$(document).ready(function() {
$("#jquery1").text($("#text1").val().length);
$("#js1").text("<?php echo str_replace(array("\n", "\r"), array('\n', '\r'), $str1) ?>".length);
$("#jquery2").text($("#text2").val().length);
$("#js2").text("<?php echo str_replace(array("\n", "\r"), array('\n', '\r'), $str2) ?>".length);
});
</script>
For the first box it gives me jQuery: 11, JS: 11, PHP: 11, but for the second box I get jQuery: 11, JS: 12, PHP: 12.
There are several solutions I can think of (none of which is ideal):
Use a Zend_Filter_PregReplace in your form to replace all \r\n with \n. Pro: Counting will be consistent with that of jQuery's val() and relatively easy. Con: You are destroying the user's line break which might lead to unwanted results.
Decorate the Zend_Validate_StringLength so that you can replace \r\n by \n in the isValid() method. Pro: Will preserve the user's line break, Con: You might get a valid result that is longer then 200 characters, because \r\n is counted as one character and you need to introduce a new class.
Use the jQuery's textarea.valHooks to replace all line breaks with \r\n: Pro: Simple, Con: If you have users that have \n as line break char, it again will give you inconsistent results.
I hope this answer show you some directions on how you can tackle this situation depending on your app's context.

What is the reason for the error "Failed to decode JSON" in MediaWiki::API?

We have private MediaWiki installation inside our company. Based on daily builds on our source code, we update the wiki with Perforce labels so that people can use the build that is labeled for streamlined process. We tried to automate this using Perl scripts on a Windows server using MediaWiki::Bot and MediaWiki::API.
use MediaWiki::Bot;
use MediaWiki::API;
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'http://somewiki/w/index.php/title#feature_List';
# log in to the wiki
$mw->login({
lgname => 'username',
lgpassword => 'password'
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# get a list of articles in category
my $articles = $mw->list({
action => 'query',
list => 'categorymembers',
cmtitle => 'Category:Perl',
cmlimit => 'max'
}) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# and print the article titles
foreach (#{$articles}) {
print "$_->{title}\n";
}
Output:
2: Failed to decode JSON returned by http://vaporwiki/w/index.php/Executor#Execu
tor_Feature_List
Decoding Error:
malformed JSON string, neither array, object, number, string or atom, at charact
er offset 0 (before "<!DOCTYPE html PUBLI...") at C:/Perl/lib/MediaWiki/API.pm l
ine 398
Returned Data: <whole page data>
The API URL is wrong. Try http://vaporwiki/w/api.php.

Facebook SDK fan page app issue

In the last couple of days I've been building a new Facebook app that lets a user add a PHP CMS to their fan page that lets them build a full 'minisite' without any design or programming knowledge.
But, when I try to get the current page ID that the app is installed on, I don't get anything.
I'm using both HTTP and HTTPS, but I get nothing from either of them.
The code I'm trying to pull the data with is:
require "src/facebook.php";
$app_id = “MY APP ID”;
$app_secret = “MY APP SECRET”;
$facebook = new Facebook(array(‘appId’ => $app_id,
‘secret’ => $app_secret,
‘cookie’ => true));
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
echo "<br>". $pageid;
echo "<br>". $page_admin;
echo "<br>". $like_status;
And im having these error messages:
Notice: Use of undefined constant “247517918644221” - assumed '“247517918644221”' in /home/hazvuv/hazvuvp.com/why/index.php on line 6
Notice: Use of undefined constant “d7750be2ef0934bea4e2e8fe65a42a2a” - assumed '“d7750be2ef0934bea4e2e8fe65a42a2a”' in /home/hazvuv/hazvuvp.com/why/index.php on line 7
Notice: Use of undefined constant ‘appId’ - assumed '‘appId’' in /home/hazvuv/hazvuvp.com/why/index.php on line 9
Notice: Use of undefined constant ‘secret’ - assumed '‘secret’' in /home/hazvuv/hazvuvp.com/why/index.php on line 10
Notice: Use of undefined constant ‘cookie’ - assumed '‘cookie’' in /home/hazvuv/hazvuvp.com/why/index.php on line 11
Notice: Undefined index: appId in /home/hazvuv/hazvuvp.com/why/src/base_facebook.php on line 213
Notice: Undefined index: secret in /home/hazvuv/hazvuvp.com/why/src/base_facebook.php on line 214
Notice: Undefined variable: pageid in /home/hazvuv/hazvuvp.com/why/index.php on line 22
What am i doing wrong?
Your script has unicode quotes and double quotes and PHP does not recognize them. See ‘ vs ' and ” vs ". It is hard to tell but they are different. Go through the code and replace the unicode quotes/double quotes with ascii quotes manually typed from you keyboard.
replace quotations marks (“ ” ‘ ’) with regular quotes (' and ")

Twitter RSS feed, [domdocument.load]: failed to open stream:

i'm using the following:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate">#' . $newDate .'</div>
</div>
</div>';
}
return $tweetsBox;
?>
to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)
It seems to work fine but i've recently been getting the following error sporadically:
PHP error debug
Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.
thanks.
I know this is old, but I was just searching for the same solution for a nearly identical script for grabbing a twitter timeline. I ended up doing this, though I haven't been able to thoroughly test it.
I defined the twitter url as a variable ($feedURL), which I also used in $doc_load. Then, I wrapped everything except for the $feedURL into this conditional statement:
$feedURL = "http://twitter.com/statuses/user_timeline/XXXXXXXX.rss"
$headers = #get_headers($feedURL);
if (preg_match("/200/", $headers[0])){
//the rest of you original code in here
}
else echo "Can't connect user-friendly message (or a fake tweet)";
So, it's just checking the headers of the the feed's page, and if its status is 200 (OK), then the rest of the script will execute. Otherwise, it'll echo a message of your choice.
(reference: http://www.phptalk.com/forum/topic/3940-how-to-check-if-an-external-url-is-valid-andor-get-file-size/ )
ETA: Or even better, save a cached version of the feed (which will also ensure you don't go over your API limit of loads):
<?php
$cache_file = dirname(__FILE__).'/cache/twitter_cache.rss';
// Start with the cache
if(file_exists($cache_file)){
$mtime = (strtotime("now") - filemtime($cache_file));
if($mtime > 600) {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
echo "<!-- twitter cache generated ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
}
else {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
//End of caching
?>
Then use $cache_file in your $doc->load($cache_file) statement instead of the actual feed url.
(Adapted from here: http://snipplr.com/view/8156/twitter-cache/).