Pop up on button click - javascript

I am trying to make it so when a button is clicked it creates a pop up however I have this error "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\project\calendar_start.php on line 55"
Code:
<?php
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$showmonth= preg_replace('#[^0-9]#i', '', $showmonth);
$showyear= preg_replace('#[^0-9]#i', '', $showyear);
$day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear);
$pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6-(date('w', mktime(0,0,0, $showmonth, $day_count, $showyear))));
echo '<div id="calendar_wrap">';
echo '<div class="title_bar">';
echo '<div class="previous_month"><input name="button" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>';
echo '<div class="show_month">' . date('F', mktime(0, 0, 0, $showmonth)) . ' ' . $showyear . '</div>';
echo '<div class="next_month"><input name="button" type="submit" value="Next Month" onClick="javascript:next_month();"></div>';
echo '</div>';
echo '<div class="week_days">';
echo '<div class="days_of_the_week">Sun</div>';
echo '<div class="days_of_the_week">Mon</div>';
echo '<div class="days_of_the_week">Tues</div>';
echo '<div class="days_of_the_week">Wed</div>';
echo '<div class="days_of_the_week">Thur</div>';
echo '<div class="days_of_the_week">Fri</div>';
echo '<div class="days_of_the_week">Sat</div>';
echo '<div class="clear"></div>';
echo '</div>';
if ($pre_days != 0) {
for($i=1; $i<=$pre_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
$con=mysql_connect("localhost","root","");
mysql_select_db("familease", $con);
for ($i=1; $i<= $day_count; $i++) {
$date = $i.'/'.$showmonth.'/'.$showyear;
//echo $date;
$query = "Select id FROM events WHERE evDate = '$date'";
$num_rows = 0;
// check if the query returns anything
$result = mysql_query($query,$con) or die(mysql_error());
if ($result)
{
$num_rows = mysql_num_rows($result);
}
if($num_rows > 0) {
<input type="submit" value="Details"
onClick="window.open('events.php', 'View',width=300,height=200,menubar=yes,status=yes)"">
}
echo '<div class="cal_day">';
echo '<div class="day_heading">' . $i . '</div>';
if($num_rows > 0)
{
echo "<div class='openings'><br/>" . $event . "</div>";
}
echo '</div>';
} //end of for loop
if ($post_days !=0) {
for($i=1; $i<=$post_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
echo '</div>';
?>

With no more context all I can see is that you use only simple quotes (') inside your onclick statement. Use double quotes or escape simple quotes inside this statement such as :
onClick="window.open('events.php', 'View',width=300,height=200,menubar=yes,status=yes)"

Related

Json foreach loop is outputting the word object instead of its contents

I am attempting to send the foreach loop through json and then display with innerHTML. There aren't any errors. The issue I am having is that the output is display:
[object Object]
Instead of what is in the foreach loop.
How do I get the output to be the $html lines that are within the foreach loop?
try {
$sql_recentProjects = "
SELECT *
FROM project_gallery
ORDER BY date_added ASC
LIMIT 5
";
if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
$recentProjects_stmt->execute();
$recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
$recProj_arr = array();
foreach ($recentProjects_rows as $recentProjects_row) {
$precProjName = $recentProjects_row['p_name'];
$recProjImg = $recentProjects_row['p_img'];
//$project_img = substr($project_img, 2);
$displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
$html = '';
$html .= '<div class="recentProjectCont">';
$html .= '<div class="recentProjectImg">';
$html .= $displayRecProjImg;
$html .= '</div>';
$html .= '<div class="recProjInfoCont">';
$html .= '<div class="">';
$html .= $precProjName;
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$recentProjData = array('html' => $html);
//$proj_arr[] = $data;
}
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);
JS
function ajaxCallCatalogs() {
$.ajax({
url: 'php/projects/projectGallerySelect.php',
datatype: 'json',
success: function (data) {
//console.log(data);
obj = JSON.parse(data);
recentProjectData = obj.recentProjectData; //Recent 5 Projects submitted to gallery
document.getElementById('recentProjectWrap').innerHTML = recentProjectData;
}
});
}
ajaxCallCatalogs();
setInterval(ajaxCallCatalogs, 150000);
(Read the comments of the OP to know why I posted this answer)
You have two options... the easiest way, but more restrictive method is this:
try {
$sql_recentProjects = "
SELECT *
FROM project_gallery
ORDER BY date_added ASC
LIMIT 5
";
if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
$recentProjects_stmt->execute();
$recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
$recProj_arr = array();
$html = ''; // MOVE THIS OUTSIDE THE LOOP
foreach ($recentProjects_rows as $recentProjects_row) {
$precProjName = $recentProjects_row['p_name'];
$recProjImg = $recentProjects_row['p_img'];
//$project_img = substr($project_img, 2);
$displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
$html .= '<div class="recentProjectCont">';
$html .= '<div class="recentProjectImg">';
$html .= $displayRecProjImg;
$html .= '</div>';
$html .= '<div class="recProjInfoCont">';
$html .= '<div class="">';
$html .= $precProjName;
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
//$proj_arr[] = $data;
}
// SET THIS AFTER YOU GET ALL ROWS INTO THE STRING
$recentProjData = array('html' => $html);
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);
Or, you can do this, which will truly separate the rows into more consumable data, but means you need to re-code your JS to output it proper (loop through the data via JS):
try {
$sql_recentProjects = "
SELECT *
FROM project_gallery
ORDER BY date_added ASC
LIMIT 5
";
if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
$recentProjects_stmt->execute();
$recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
$recProj_arr = array( 'html' => array() ); // SET UP THE ARRAY
foreach ($recentProjects_rows as $recentProjects_row) {
$precProjName = $recentProjects_row['p_name'];
$recProjImg = $recentProjects_row['p_img'];
//$project_img = substr($project_img, 2);
$displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
$html = '';
$html .= '<div class="recentProjectCont">';
$html .= '<div class="recentProjectImg">';
$html .= $displayRecProjImg;
$html .= '</div>';
$html .= '<div class="recProjInfoCont">';
$html .= '<div class="">';
$html .= $precProjName;
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$recentProjData['html'][] = $html; //ADD TO THE ARRAY, INSTEAD OF OVERWRITING IT
//$proj_arr[] = $data;
}
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);

Jquery Clone select not working properly

In my code of wordPress I am trying to clone <select> box with its options it works but the problem is all of its option went outside ending select tag </select> I am not confirm whats wrong with it
Preview Image
HTML Output: https://jsfiddle.net/h5voq4nt/
PHP Code
$output .= '<label for="_jwppp-video-url-' . $number . '">';
$output .= '<strong>' . __( 'Media URL', 'jwppp' ) . '</strong>';
$output .= '<a class="question-mark" href="http://www.ilghera.com/support/topic/media-formats-supported/" title="More informations" target="_blank"><img class="question-mark" src="' . plugins_url('jw-player-7-for-wp-premium') . '/images/question-mark.png" /></a></th>';
$output .= '</label> ';
$output .= '<p>';
$output .= '<input type="text" id="_jwppp-video-url-' . $number . '" name="_jwppp-video-url-' . $number . '" style="margin-right:1rem;" placeholder="' . __('Video (YouTube or self-hosted), Audio or Playlist', 'jwppp') . '" ';
$output .= ($video_url != 1) ? 'value="' . esc_attr( $video_url ) . '" ' : 'value="" ';
$output .= 'size="60" />';
$output .= '<input type="text" name="_jwppp-' . $number . '-main-source-label" id ="_jwppp-' . $number . '-main-source-label" class="source-label-' . $number . '" style="margin-right:1rem;';
$output .= '" value="' . $main_source_label . '" placeholder="' . __('Label (HD, 720p, 360p)', 'jwppp') . '" size="30" />';
$output .= '<select style="margin-top: 0; margin-left: 0.8rem;" id="_jwppp-video-ad-' . $number . '" name="_jwppp-video-ad-' . $number . '" />';
$output .= '<option name="NoAds" value="NoAds"';
$output .= ($ads_client == 'NoAds') ? ' selected="selected"' : '';
$output .= '>No Ads</option>';
$output .= '<option name="AdCode1" value="AdCode1"';
$output .= ($ads_client == 'AdCode1') ? ' selected="selected"' : '';
$output .= '>Ad Code 1</option>';
$output .= '<option name="AdCode2" value="AdCode2"';
$output .= ($ads_client == 'AdCode2') ? ' selected="selected"' : '';
$output .= '>Ad Code 2</option>';
$output .= '<option name="AdCode3" value="AdCode3"';
$output .= ($ads_client == 'AdCode3') ? ' selected="selected"' : '';
$output .= '>Ad Code 3</option></select>';
JQuery Code
<script>
(function($) {
$(document).ready(function() {
var number = <?php echo $number; ?>;
var $url = $('#_jwppp-video-url-' + number).val();
var $ads = $('#_jwppp-video-ad-' + number).val();
var $ext = $url.split('.').pop();
var $arr = ['xml', 'feed', 'php', 'rss'];
//CHANGE PLAYLIST-HOW-TO
var tot = $('.jwppp-input-wrap:visible').length;
if(tot > 1) {
$('.playlist-how-to').show('slow');
var string = [];
$('.order:visible').each(function(i, el) {
string.push($(el).html());
})
$('.playlist-how-to code').html('[jw7-video n="' + string + '"]');
} else {
$('.playlist-how-to').hide();
}
$('.jwppp-more-options-' + number).hide();
if($.inArray($ext, $arr)>-1) {
$('.more-options-' + number).hide();
};
$('#_jwppp-video-url-' + number).on('change',function() {
var $url = $('#_jwppp-video-url-' + number).val();
var $ads = $('#_jwppp-video-ad-' + number).val();
var $ext = $url.split('.').pop();
var $arr = ['xml', 'feed', 'php', 'rss'];
if($.inArray($ext, $arr)>-1) {
$('.more-options-' + number).hide();
$('.jwppp-more-options-' + number).hide();
} else {
$('.more-options-' + number).show();
}
});
});
})(jQuery);
</script>
In PHP code has
$output .= '<select style="margin-top: 0; margin-left: 0.8rem;" id="_jwppp-video-ad-' . $number . '" name="_jwppp-video-ad-' . $number . '" />';
this has an end slash (/), please remove it.

Does repeated use of the fgetcsv function in this case overwrite the "$line" array?

Does it overwrite the $line array? If yes, will renaming the array fix the problem? Furthermore, how can I call data from specific fields in this array through jquery?
I need to make an INTERACTIVE VISUAL to represent all the data being read from these files. How can I go about doing this?
This is the php code:
<?php
echo "<html><body><table>\n\n";
$f = fopen("LoanStats3a.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$g = fopen("LoanStats3b.csv", "r");
while (($line = fgetcsv($g)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($g);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$h = fopen("LoanStats3c.csv", "r");
while (($line = fgetcsv($h)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($h);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$i = fopen("RejectStatsA.csv", "r");
while (($line = fgetcsv($i)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($i);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$j = fopen("RejectStatsB.csv", "r");
while (($line = fgetcsv($j)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($j);
echo "\n</table></body></html>";
?>

Show div when variable (time) is 0

I'm trying to show div when time (importing from database) - current time will be 0. How to do that? Here's the code:
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{
echo "<script>";
echo "$(document).ready(function(){";
echo "$('#". $row['id'] . "').show();";
echo "$('#". $row['id'] . "').delay(15000);";
echo "$('#". $row['id'] . "').hide();";
echo "});";
echo "</script>";
}
}
I think you want to add a setTimeout() to your javascript code instead of if ($differenceInSeconds==0) {} PHP side.
Also you need to rewrite your jQuery from (effectively showing an element, delaying nothing and hiding it right away)
$("#element").show();
$("#element").delay(15000);
$("#element").hide();
to
$("#element").show().delay(15000).hide(0);
Full code below.
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
// Adding a setTimeout so your JS code actually gets written and executes when it should.
if ($differenceInSeconds >= 0) {
echo "<script>";
echo "$(document).ready(function(){";
echo "window.setTimeout(function() {";
echo "$('#". $row['id'] . "').show().delay(15000).hide(0);";
echo "}, " . $differenceInSeconds * 1000 . ");";
echo "});";
echo "</script>";
}
}
Try it :-
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{ ?>
<script>
$(document).ready(function(){
$('#<?php echo $row['id'] ?>').show();
$('#<?php echo $row['id'] ?>').delay(15000);
$('#<?php echo $row['id'] ?>').hide();
});
</script>
<?php }
}
?>

Detect difference of form submit via submit button or javascript submit function in PHP

I am using a date picker in PHP, but I experience a small problem.
When selecting a month, I automatically submit the form to change the amount of days:
<script>
function change()
{
document.getElementById("datepickerform").submit();
}
</script>
But, when I press submit, I also want to execute some extra code (querying a database for some info with the selected date.
Is there a way to make a difference when the OK button is pressed or when the form is "submitted" via the function above?
Hereunder is the entire code (not finished, leap year also not included yet) of my datepicker.php:
<script>
function change(){
document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// http://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page
// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);
// Get result from FORM POST
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
$currentday = $submittedday;
$currentmonth = $submittedmonth;
$currentyear = $submittedyear;
}
else
{
$currentday = intval(date("d"));
$currentmonth = intval(date("m"));
$currentyear = intval(date("Y"));
}
//DEBUG ON
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
// DEBUG OFF
echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentday)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentmonth)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentyear)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>
Could anybody help?
Most JS libraries that preform AJAX calls pass an extra header with the request, typically X_REQUESTED_WITH with a value of XMLHttpRequest. If you were using something like jQuery you can check for this using PHP, or you could just use a hidden field that your JS creates and fills in when it is submitted that way.
function change()
{
var form = document.getElementById('datepickerform'),
el = document.createElement('input');
el.type = 'hidden';
el.name = 'js';
el.value = '1';
form.appendChild(el);
form.submit();
}
And then when handling in PHP, use
if (isset($_POST['js'])) {
// ...
}
You can Keep a hidden field in form and if the form is beig submitted using js then change the value of hidden field before submitting the form.
You could change it so that there is an Ajax call when the month changes, and use a button that can be used to invoke processing prior to submit.
Thanks for the above tips, this solved my problem. Also Change value of input then submit form in javascript helped solving my problem.
<script>
function change(){
document.getElementById("datepickerform").submitform.value = '1';
document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// https://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page
// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);
// Get result from FORM POST
$submitform = $_POST["submitform"];
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
$currentday = $submittedday;
$currentmonth = $submittedmonth;
$currentyear = $submittedyear;
}
else
{
$currentday = intval(date("d"));
$currentmonth = intval(date("m"));
$currentyear = intval(date("Y"));
}
//DEBUG ON
echo $submitform."<br>\r\n";
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
if ($submitform == '0')
{
echo 'Button pressed.<br>';
}
else
{
echo 'Javascript executed.<br>';
}
// DEBUG OFF
echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentday)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentmonth)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentyear)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="hidden" name="submitform" value="0">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>

Categories

Resources