Cannot able to submit the form automatically - php

I have written this code. Here after logging into facebook, the document needs to redirect to check3.php. Here is the code..
<html>
<body>
<div id="header">
<h1>ReviewBox</h
<ul id="nav">
<li>Real Reviews For Real People</li>
<li style="background: url(images/request.png);display:block; margin : 0 10px 0 0;float:right;text-indent: -9999px; width : 200px; height: 30px;">Request Review From Friends</li><br/>
</ul><br/><br/><br/><br/></div>
<div id="container">
<div id="content">
<p>Someone is always out there, waiting to know what you have to say.
So why not speak out with ReviewBox?</p><br/>
<h6>Get Started!</h6><br/><br/>
<center>
<div id="fb-root"></div>
<script language="javascript" src="json2.js"> </script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'xxxxxxxxxxxx', cookie:true,
status:true, xfbml:true
});
</script>
<fb:login-button perms="read_stream,publish_stream,email,offline_access">
Login with Facebook
</fb:login-button>
<br/><br/>
<p>Like ReviewBox :</p><br/><center>
<fb:like href="https://www.facebook.com/apps/application.php?id=xxxxxxxxxx" send="false" width="220" show_faces="true" font=""></fb:like></center>
<script>
FB.api('/me',function(response) {
setCookie("fbname",response.name,365);
setCookie("fbid",response.id,365);
setCookie("checker",1,1);
var username=getCookie("fbid");
if (username!="undefined" && username!="")
{
alert("Welcome Again, " + response.name + "!");
sample();
//window.location.reload();
}
else
{
alert("Welcome Guest! Please Login with Facebook To Get Started With ReviewBox!");
username=response.id;
username2=response.name;
if (username!=null && username!="")
{
setCookie("fbid",username,365);
setCookie("fbname",username2,365);
setCookie("checker",1,1);
}
}
});
function sample()
{
document.form1.submit();
}
</script>
<form name="form1" action="check3.php" method="post">
<input type="hidden" name="fb_user" id="fb_user" value="" />
</form>
</body>
</html>
But when I run this code and after logging into facebook the code inside sample() function is not executed.. as a result my page does not redirects to check3.php.
Any suggestion?

Learn,
First of all, determine whether or not the Facebook reponse object actually contains a value. It is possible that the API is for some reason, not returning an ID result?
Once you verify that the API is indeed returning a value, perhaps use that in your "if" statement, instead of username?
I don't know exactly what is wrong, but I am fairly certain that the "fbid" value is not being set inside the cookie, which to me implies that the API is not returning a correct result, or you are passing in invalid parameters to the API.
Good luck!
H

Related

padding a return value from database

I use this code to revieve some data from my database. The database has one column, that are numbers from 0 - 40. I get my return numbers from the database in a DIV tag. I would like to sort the numbers, so
1 - 20 is going on the left side of the div tag
21 - 40 is going on the right side of the div tag
0 is going in the middle of the div tag
I am thinking if it is possible to put a padding on some specifik numbers, or which way is the smartest to do it?
EDITED...
Thanks for the answers guys. The code is quite amateur, but I am not so good at it yet. But the return of the database is ok, except there is comming a 0 everytime. In the left handside where the numbers in the div are, that is where I want to padding some specifik numbers.
I hope that clearlifies my question a little bit?
Best Regards
Julie
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/my_script.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css\placing.css">
<title>Numbers</title>
</head>
<body>
<div class="topbar">
<p>Topbar</p>
</div>
<div class="talraekke" id="show">
<p>Tal</p>
</div>
<div class="content">
<p>Enter The Number</p>
<form id="myForm" action="select.php" method="post">
<input type="number" name="numbervalue">
<button id="sub">Save</button>
</form>
<span id="result"></span>
</div>
<div class="talraekke">
<p>test</p>
</div>
<div class="talraekke">
<p>test</p>
</div>
</body>
</html>
JS:
// Insert function for number
function clearInput() {
$("#myForm :input").each( function() {
$(this).val('');
});
}
$(document).ready(function(){
$("#sub").click( function(e) {
e.preventDefault(); // remove default action(submitting the form)
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
clearInput();
});
})
// Recieve data from database
$(document).ready(function() {
setInterval(function () {
$('#show').load('response.php')
}, 3000);
});
PHP Response from DB
// Return data from database
$result = $conn->query("SELECT numbers FROM numbertable");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row['numbers'] . '<br>';
}
As I understood you should use this simple way:
<?php
for($i=0; $i<=40; $i++)
{
echo '<div class="test">'.$i.'</div></br>';
}
?>
<style type="text/css">
.test{
background-color:#284062;
text-align:center;
color:#fff;
min-width:30px;
width:32px;
}
</style>

Opening a Modal Window "using" PHP

I am creating a log in system in PHP and I'm trying to make it a little nicer.
When you log out, you get redirected back to index.php. Like this:
header("loaction: index.php?logout=true")
This makes the url look like www.mysite.com/index.php?logout=true. I then use the following code:
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
to get the value of logout from the URL and do something with it.
I have a small popout window that will display the notification variable's value. In this case it's "You've been logged out!" My question is how do I get the modal window to display when the page loads and when the url equals /index.php?logout=true ?
All comments, answers, and suggestions are greatly appreciated!
Thanks!
- Ryan
First of all,
You can't "Open a Modal Window using PHP" directly.
You can only do this only by exchanging variables (via JSON or XML), or embedding PHP conditions right into your markup.
PHP and JavaScript are independent.
My question is how do I get the modal window to display when the page
loads and when the url equals /index.php?logout=true ?
There are two ways you can achieve this.
First: Make a good use of embedding PHP conditions right into the markup.
Second: Have somewhere hidden input, like (<input type="hidden" id="showModal" />) and then check if it exists via JavaScript itself.
For example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function(){
if ( document.getElementById('showModal') ){
alert('Box'); //replace with your own handler
}
}
</script>
</head>
<body>
<?php if ( isset($_GET['logout']) && $_GET['logout'] === 'true' ): ?>
<input type="hidden" id="showModal" />
<?php endif;?>
</body>
</html>
Are you looking for something like:
<script>
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
echo 'alert("You\'ve been logged out!");'
}
?>
</script>
EDIT: I believe you are looking for something like this for a modal window (using jQuery)
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
$message = "You've been logged out!";
?>
<script>
$(function() {
$("#dialog").dialog();//if you want you can have a timeout to hide the window after x seconds
});
</script>
<div id="dialog" title="Basic dialog">
<p><?php echo $message;?></p>
</div>
<?php } ?>
I know it is an old post, but to help someone in future with similar quest, I guess the following may help to get into right directions ( I have tested the below code myself, so please make adjustment).
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
$message = "You've been logged out!";
?>
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<div class="fetched-data"><?php $message ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
Ok, so your existing code is like this snippet, and the problem you're having is that you want to show a jQuery/Bootstrap modal triggered by/with PHP, but jQuery/JS hasn't loaded yet (so you'll get a "$ undefined" error).
When you say "a small popout window" I'm assuming you mean a bootstrap type modal.
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
}
What I did was move the PHP code block to the end of the .php file, after jQuery (etc), and have an include, so it would look like this (assuming the name of your modal is id="logoutModal"):
logout.php
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
include("logout-modal.php");
}
}
logout-modal.php
<?php ?>
<script>
$('#logoutModal').modal('show');
</script>
<?php ?>
Not 100% sure this answers your question, but this works for me. Hope this helps.
Best way to get done this by using the PRG pattern.
index.php
1. Create the element for modal content in HTML
<div id=modal></div>
2. Style your #modal element
#modal {
position: fixed;
display: none; /*important*/
color: white;
text-align: center;
z-index: 1000;
width: 100%;
height: 80px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: crimson;
line-height: 2;
}
3. Define function how to show and disappear the modal box using jQuery
'use strict'
window.jQuery ?
$.fn.notify = function(interval) {
let el = $(this),
text = el.text(),
modal =()=> { // this will fire after the interval has expired
el.fadeOut(),
clearInterval(timer)
},
timer = setInterval(modal,interval)
!text.match(/\S+/) || el.text('') // this will fire when modal box contains text
.append(`<h3>${text}</h3>`)
.show()
}
: alert('jQuery required.')
4. Attach .notify() to the jQuery selector
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
5. Insert the notification sent from PHP inside the #modal element
<div id=modal>
<?=
#$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
6. Perform a simple $_GET request
<a href=parse.php?logout>Logout</a>
parse.php
7. Return a value using PHP in a $_SESSION variable
if (isset($_GET["logout"])) {
$_SESSION["notify"] = "You have been logged out.";
header("Location: index.php");
}
This is good practice. Once the logout request is sent to PHP, it will redirect to the index page with the result in a session variable. The PRG pattern does not always require a POST request. This example sends a GET request which is ok for logouts. Note that you have to place session_start(); at the top of your files.
Or you could even write the script inside if statement:
<?php
if(isset($_GET['logout']) && $_GET['logout'] === 'true'){ ?>
<script type="text/javascript>
window.open(yourhtml);
</script>
</php }
else {
// may be some other script
}
?>
Please download a Modal JS Library http://simplemodal.plasm.it
and Use the Condition
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
code to open the Modal as in the JS library.....
}

AJAX load PHP file

I am a newbie on AJAX, I have a link that loads table.php. Then it writes the code to the index.php. In that code, I have another link to
show the info.php. Is it possible to do this?
<!--This is index.php-->
<div id="link">my Info</div><!--it works here-->
<div id="link">My Table</div>
<div id="table"></div>
<div id="info"></div>
<!--My javascript-->
<script type="text/javascript">
$(document).ready(function() {
$('#link a').click(function(){
var page = $(this).attr('href');
if(page =='table')
$('#table').load('table.php');
else if(page =='info')
$('#info').load('info.php');
return false;
})
});
</script>
<!--This is table.php-->
<div id="link">my Info</div><!--it doesn't works here-->
<!--This is info.php-->
<h1>My info</h1>
Your three <div> (as pointed out by #scragar) have the same id link, most probably causing the issue. Make it a class like that :
<div class="link">
And in your JS :
$('.link a')
EDIT : As noted by dbf, you must as well declare your handler with live() or on() instead of click() :
$('.link a').live('click', function(){ ... });
in order for it to be binded after table.php is loaded in the page. ( http://api.jquery.com/live/ )

connection between php javascript for alert function with string

I am working on site for users to do social netwoking. like facebook. or twitter. So far i have this code and let me show you what I want to do.
<button id="welcome">Welcome user</button>
<?php
function dal(){
alert("Hello user, welcome to my big social network")
}
?>
and i do javascript like this
<script>
document.byID('welcome').onclick = dal();
</script>
code currently is not working.
if you have any suggestion i really appreciate.
Remove the php and write this inside script tag
<script>
window.addEventListener("DOMContentLoaded",
function(){
document.getElementById('welcome').onclick=dal;
},false);
function dal(){
alert("Hello user, welcome to my big social netowkr")
}
</script>
You can't call php functions in javascript.
<button id="welcome" onclick="dal()">Welcome user</button>
<script type="text/javascript">
function dal()
{
alert("Hello user, welcome to my big social network")
}
</script>
This question really needs rewording. I honestly don't know where to start correcting the mistakes with this.... however to make the alert work for your button you need the following:
<button id="welcome">Welcome user</button>
<script>
document.getElementbyID('welcome').onclick = function(){
alert("Hello user, welcome to my big social network");
}
</script>
also you can't call or access PHP in JavaScript.
Please note the spelling mistake in your alert message too
It is considered best practice to unobtrusively add the handlers in the head instead of inline
I suggest
<?php ... ?>
<html>
<head>
<script>
window.onload=function() {
document.getElementById('welcome').onclick=function() {
alert("Hello user, welcome to my big social network")
}
}
</script>
</head>
<body>
<div id="content">
<button id="welcome">Welcome user</button>
</div>
</body>
</html>

Jquery mobile - div content bind scroll not working the first time but after refreshing the page it works

<div data-role="page" id="one" data-theme="a" >
<div data-role="content" id="content">
<script type="text/javascript" >
$(document).ready(function() {
$('#content').bind('scroll', function () {
alert(0);
});
});
In this i have set a alert on scroll, but its not working but its working after manual refresh of page.
Dont know whats the reason? Any help.
PS: the div's CSS property of position is "absolute" - Does this anything to do with the above problem?
Your problem is somewhere else, the following jsfiddle works without problems http://jsfiddle.net/mendesjuan/jBeE6/
<div data-role="page" id="one" data-theme="a" >
<div data-role="content" id="content">
<script type="text/javascript" >
$(document).ready(function() {
$('#content').bind('scroll', function () {
console.log("Here it is");
});
});
</script>
Here is a lot of content... a lot more goes here
</div>
</div>​

Categories