how to document.getElementById().innerHTML from different html/php file - javascript

Hello I am really new to coding in general but I got the basics of what I need.
I have my index.html that contains this:
<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="hidden">
</li>
<li class="page-scroll">
Portfolio
</li>
<li id="navbutone" class="page-scroll">
Login
</li>
<li id="navbuttwo" class="page-scroll">
Register
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
keep in mind I got this from a website template that I am editing so I didn't come up with this layout
and I have a php file that has some html in it to try and replace the contents of the list when this part of the code is run:
<?php
if($login_ok)
{
?>
<script type="text/javascript">
function logedin() {
document.getElementById("one").innerHTML = "Logout";
}
</script>
<script type="text/javascript">
logedin();
</script>
<?php
header("Location: index.html");
die("Redirecting to: private.php");
}
?>
This doesn't work and I have no idea if this is even close or not.
Thanks in advance for the help.
also I might add that they link to login.php where they login through an html form at the bottom of the php.
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" value="<?php echo $submitted_username; ?>" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
Register
<script src="index.html"></script>
</html>
Update: I found what I needed instead of messing with the php file i just put this into my index.html were the links will change out:
<?php
require("common.php");
if(empty($_SESSION['user']))
{
?>
<li class="page-scroll">
Login
</li>
<li class="page-scroll">
Register
</li>
<?php
}
else
{
?>
<li class="page-scroll">
Logout
</li>
<li class="page-scroll">
Members Page
</li>
<?php
}
?>
were common.php just connects to my database.

Look, I'm gonna give you some tips you could use for developing this:
First of all, you should use only PHP files (index.php instead of index.html), so it's easier to manage POST data and session variables.
So:
index.php
<?php
// This is PHP code, executed BEFORE any output is sent.
// First, to save data that works across page loads, we should use sessions, so we start a session that has to be called in every PHP page that uses that information.
// Variables use the format $_SESSION['variable_name'] = value
session_name('MySession'); // Give it a unique name
session_start(); // Start a session
?>
<html>
<head>
<title>Some title for your page...</title>
</head>
<body>
<!-- Here you will manage your template. It's plain HTML but, as this is a PHP file, you can include PHP code as well inside the PHP tags -->
<?php
// This is a PHP tag, here we can manage some PHP and output different HTML
// We check if the user logged in or not
if (
isset($_SESSION['logged_in']) // Always check if a variable exists before checking its value, or PHP will complain
&&
$_SESSION['logged_in'] == true
)
{
// The user logged in, show a LOGOUT link
echo '<a href=logout.php>Logout</a>';
}
else
{
// Otherwise, the user did not log in. Show a link to log in.
echo '<a href=login.php>Login</a>';
}
?>
<!-- Any other HTML you want, template or whatever -->
</body>
<html>
Now, we used two files: login.php and logout.php. The first one will show a form, the second one will logout and redirect to the index page.
login.php
<html>
<head>
<title>Please log in</title>
</head>
<body>
<form action="do_login.php" method="post"><!-- Notice another file: do_login.php -->
<input type="text" name="username" placeholder="Your username" />
<br />
<input type="password" name="password" placeholder="Your password" />
<br />
<br />
<input type="submit" name="submit" value="Log in" />
</form>
<body>
</html>
Now we need the file that processes the login (do_login.php in the form) and stores session data.
do_login.php
<?php
// We use the same session as before
session_name('MySession'); // Same name as index.php and all other files
session_start();
// This will be a pure PHP file that stores session data and returns to the index page.
// You want to check data against databases here, but we will use static information for easier reading.
// You also want to check data to be correct, but we won't do that here for simplicity.
$username = $_POST['username']; // This is the "username" from the form.
$password = $_POST['password']; // This is the "password" from the form.
if (
$username == 'John' // Username is John
&&
$password == 'MyPassword' // Password is MyPassword
)
{
// Here the login data is correct, let's save some session variable that says the user correctly logged in.
// Note that this is potentially extremely INSECURE! You should save other data and check every request, but this is just for you to start learning.
$_SESSION['logged_in'] = true;
// Ok, user logged in. Redirect to the index.
header('Location: index.php'); // Send a redirect header (note that NOTHING has been echoed before in this page).
exit;
}
else
{
// Login data incorrect. Redirect to an error page, let's say login_error.php
header('Location: login_error.php');
exit;
}
?>
Now the file to log out:
logout.php
<?php
// First we recreate the session and destroy the variable(s) that say the user has logged in.
session_name('MySession'); // Same name as before
session_start(); // We start the session. At this point, all session variables have been recreated.
unset( $_SESSION['logged_in'] ); // We destroy the variable
session_destroy(); // Now we drop the session
header('Location: index.php'); // Redirect to index.php
exit;
?>
Now we only need the login failed page:
login_error.php
<html>
<head>
<title>Login error!<title>
</head>
<body>
<h1>Login error!</h1>
<p>The login data was incorrect. Try again.</p>
<br />
<p>Go back to the index page</p>
</body>
</html>
I hope this helps, but you really need to read some tutorials.
Have fun!

Use used " instead you should have used '
<script type="text/javascript">
function logedin() {
document.getElementById("one").innerHTML = "<a href='logout.php'>Logout</a>";
}
</script>

Change to following line:
document.getElementById("one").innerHTML = "Logout";
" characters has to be escaped.
And link has to be like that
link
It's worth to use i.e. firefox plugins like JS console where you could see in what line error occurs.

I've got it figured out. in the index.html i just put
<li class="hidden">
</li>
<li class="page-scroll">
Portfolio
</li>
<?php
require("common.php");
if(empty($_SESSION['user']))
{
?>
<li class="page-scroll">
Login
</li>
<li class="page-scroll">
Register
</li>
<?php
}
else
{
?>
<li class="page-scroll">
Logout
</li>
<li class="page-scroll">
Members Page
</li>
<?php
}
?>
does exactly what I need and no messing with the other php files.

Related

Php home page redirect blank

I have an issue with this code. when I login it takes it does not take me to the home page but stay on the login page. I want to redirect users to (http://localhost/trial/index.php#Home) When successfully logged in. How to fix that?
<!-- Including header file which contains j query and other libraries -->
<?php include("inc/incfiles/header.inc.php"); ?>
<?php
//Check if user is logged in or not
if (!isset($_SESSION["user_login"])) {
//Verification
}
else
{
//Do nothing
}
?>
<?php
//Login Script
//user Login code
//Check user info when user inputs login information
if (isset($_POST['user_login']) && isset($_POST['password_login']) )
{
//filters input info
$user_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['user_login']);//filters everything but numbers and letters
$password_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['password_login']);//filters everything but numbers and letters
$password_login_md5 = md5($password_login); // encrypt password input because password in database is already encrypted in md5
//use Binary for case sensitive option
$sql = mysqli_query($con, "SELECT * FROM users WHERE BINARY username= BINARY'$user_login' AND password='$password_login_md5' AND closed='no' LIMIT 1"); //query
//check for existence if user exists
$userCount = mysqli_num_rows($sql); //Count the number of rows returned
//if username exists start session
if($userCount==1)
{
while($row = mysqli_fetch_array($sql)) //fecthing the row to display information
{
$id = $row["id"]; // store user id into variable called $id
}
$_SESSION["id"] = $id;
$_SESSION['user_login'] = $user_login;
$_SESSION["password_login"] = $password_login;
echo "succes!";
header("Location:#Home");
//exit("<meta http-equiv=\"refresh\" content=\"0\">");
}
else{echo"That information is incorrect, Please try again!";}
exit();
}
?>
<!-- The welcome page where users must provide login info in order to be logged in -->
<div data-role="page" id="Welcome">
<div role="main" id="loginform">
React now
<form action="" method="POST"> <!--provide username and password then submit -->
<input name="user_login" size= "25" type="text"><br /><br /><!-- Enter username /include username placeholder later-->
<input data-clear-btn="false" name="password_login" size= "25" type="password"><br /><br /><!-- Enter password /include password placeholder later-->
<input name="login" value="Login" type="submit" data-theme="a"/><!-- submit button style it later -->
</form>
<div>
Sign Up <!--Redirect user to sign up page if user not member yet-->
</div>
</div>
<div data-role="footer" data-position="fixed" data-theme="a"><!-- Footer display/ displays once-->
<h4>(C) 2016</h4> <!-- copyright symbols include later-->
</div>
</div> <!-- End of the login page-->
<!-- Sign up page where to allow users to sign up-->
<div data-role="page" id="Sign Up">
<div data-role="header" data-theme="a">
<h1>Sign Up</h1>
</div><br><br><br><br>
Sign Up for Reactr
<form>
<!-- Just left the form blank for the moment to make the code smaller and easy to read-->
</form>
<div role="main" class="ui-content">
</div>
</div><!-- End of the sign up page-->
<!-- HOME PAGE AND USER PROFILE PAGE where users can enter and submit texts-->
<div data-role="page" id="Home">
<div data-role="header" data-theme="a"><!-- Jquery settings ref included in the header file-->
<h1>Text</h1>
</div>
<!-- Allow users to search for posted texts-->
<div class="search_box">
<!-- Setting form to allow users to type text, send and search for texts-->
<form action="search.php" method="GET" id="search"><!-- Search form -->
<input type="text" name="q" size="60"/><!-- Search for text /include search placeholder later-->
</form>
</div>
<div role="main" class="ui-content">
Enter your Text<br><!-- Enter and send text -->
<input name="text-basic" id="text-basic" value="" type="text">
Send<!-- submit button with onclcick function -->
</div>
</div><!-- End of the Home page-->
</body><!-- End code-->
</html>
You have at least two problems:
1 - Most importantly, you are attempting to redirect after sending some content with PHP, which is not allowed:
echo "succes!";
header("Location:#Home");
Remove the echo line to fix this.
2 - You also need to update your redirect to redirect somewhere server-side. PHP doesn't know about client-side directives, such as named anchors on a page (in your case #Home)
header("Location:#Home");
Update this to header("Location:index.php#Home"); to resolve it. Note that if your index.php IS your home page, you may not need the #Home at all.
The # symbol in your redirect causes the issue of staying on the same page - since this indicates that its an anchor location in the page you are already in. you need to change it to:
header("Location: index.php#Home");
also - if you are simply trying to redirect your user to the top of the home page - you don't need it at all - you would only try to take them to an anchor that exists further down the page. If what you are trying to direct them to is at the top of hte page just use the relative path with no anchor listed:
header("Location: index.php");

Submit form using one button, one to emailmeform using html script and another to my internal database

Im sorry for my bad english.
Im begineer with PHP and all coding method.
I've been searching this for a days and still cannot find what I need lack to my understanding especially with php, java or ajax.
I have an emailmeform form and I have my own form on 1 php file which I want when client submit the send button, it will save the data entered previously by client to my internal database and send it to emailmeform in the same time but I cannot make this happen.
The solution I think will work is maybe ajax or javascript, but since lack of my knowledge with those codes I cannot solve this issue by my self.
Here's my code (all in 1 php script page):
<?php
session_start();
include "connection/database.php";
$sql = mysql_query("SELECT * from tb_config");
$config = mysql_fetch_array($sql);
if (isset($_POST['send'])) {
$username = $_POST['element_1'];
$password = $_POST['element_2'];
$referral = $_POST['referral'];
$a = mysql_num_rows(mysql_query("SELECT * from tb_member where username='$username'"));
if (empty($username) || empty($password)) {
echo "<script> alert('Please fill all the required form!!'); </script>";
} else if (strlen($_POST['element_2']) < 6) {
echo "<script> alert('Password at least 6 digit!!!'); </script>";
} else {
$save = mysql_query("insert into tb_member(username,password) values ('$username','$password')");
exit;
}
}
?>
<!-- this is emailmeform scipt -->
<form id="emf-form" target="_parent" class="leftLabel" enctype="multipart/form-data" method="post" action="http://www.emailmeform.com/builder/emf/to/ref">
<div id="emf-form-description"><h2>Form Register</h2></div>
</div>
<ul>
<li id="emf-li-0" class="emf-li-field emf-field-text data_container ">
<label class="emf-label-desc" for="element_0">Your Name</label>
<div class="emf-div-field"><input id="element_0" name="element_0" value="" size="30" type="text"
class="validate[optional]"/><div class="emf-div-instruction">Please fill your name</div></div>
<div class="emf-clear"></div>
</li><li id="emf-li-1" class="emf-li-field emf-field-text data_container ">
<label class="emf-label-desc" for="element_1">Username <span>*</span></label>
<div class="emf-div-field"><input id="username" name="element_1" value="<?php if (isset($_POST['element_1'])) { echo $_POST['element_1']; } ?>" size="30" type="text"
class="validate[required,length[6,15]]"/><div class="emf-div-instruction">At least 6 characters</div></div>
<div class="emf-clear"></div>
</li><li id="emf-li-2" class="emf-li-field emf-field-text data_container ">
<label class="emf-label-desc" for="element_2">Password <span>*</span></label>
<div class="emf-div-field"><input id="element_2" name="element_2" value="" size="30" type="text"
emf_mask_input="true"
class="validate[required,length[6,]]"/><div class="emf-div-instruction">At least 6 characters</div></div>
<li id="emf-li-post-button" class="middle">
<input value="Send" type="submit" name="send" onmouseover="return true;"/>
</li>
</ul>
<input name="element_counts" value="14" type="hidden" />
<input name="embed" value="forms" type="hidden" />
</form>
This script works and send to emailmeform, but wont submit any data to my internal database, BUT IF I change the action button to
<form method="post" action="">, this will submit to my internal database but not send to emailmeform. I want this work together, submit to my database and send it also to emailmeform.
I have struggling with this and still not found the answer.
Kindly please help.
Any help will be appreciated
Thanks
Refer this, it will demostrate how to insert data in database

Displaying links/buttons when a user is logged on/off

I am truly a novice at coding and only succeed with trial and error. I use a WYSIWYG program to do all the main pages in my site and then add php coding to do some specified things.What I am trying to do right now is display a log in button along with a register and forgot password links to those forms, all of which I have built and working, have this display in the masterframe page when a user is not logged in and show another set of user name, profile, logout links when they are logged in. By themselves I have all these functions working, I just cant figure out how to do it this way. Any help or steering me in the right direction to teach me would be great. If you need to be paid for your help that can be arranged as well. Thank You.
update:
This is the code that I have right now and use, again I want to have the if else statement show one thing or the other on condition, and have it show in place of, all on the masterframes page.
// have this display if user is logged in
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
}
else
{
echo 'Not logged in';
}
?></span>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
//have this display if user is logged out
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
I have tried doing this but I keep getting a syntax error for unexpected '<'
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
}
else
{
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
}
?></span>
<?php
// Setting a session variable when customer is logged in.
$_SESSION['user_loggedin'] = 1;
$_SESSION['customer_id'] = $customer_id; // Some reference of logged in customer
$_SESSION['customer_name'] = $customer_name; // Customer information collected from DB or other resource.
// Deciding whether to display "Login" button or Logged in status / links
if ($_SESSION['user_loggedin']) {
echo 'Hi ' . $_SESSION['customer_name'];
echo 'My Account';
} else {
echo 'Login';
echo ' Register';
echo ' Forgot Password';
}
If you have some PHP function to check whether customer is logged in or not, you can use that function like this in lieu of if ($_SESSION['user_loggedin']) condition
if (UserLoggedin()) {
// Logged in links
} else {
// Links to be displayed when customer is logged out.
}
You are mixing both PHP and HTML code. Please correct.
You have to separate / embed HTML properly in your document while using php conditions.
Example:
<?php
$condition = true;
if ($condition) {
?>
<h1>This will be displayed when condition is true</h1>
<?php
} else {
?>
<h1>This will be displayed when condition is false</h1>
<?php
} // else ends
?>
Please try this:
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
?>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
<?php
}
else
{
?>
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
<?php
}
?></span>
Adding some code in your question would be nice, but if I understand your question correctly you might want to try this:
$logged_in = 0;
if($logged_in == 0) {
//all the stuff you want to show when the person is not logged in
$logged_in = 1;
}
if($logged_in == 1) {
//all the stuff you want to show when the person is logged in
}
In order to do what you are trying, you need to implement just a bit of logic on the code. The example of kerv is perfectly valid. The idea is that you will validate if the user is logged in or not, before rendering the html. For example:
if($userLoggedIn){
<div> Welcome to the site </div>
} else {
<div> Your are not logged in, please do so to continue </div>
}
I'll suggest you to edit the question with some code so we can properly help you.
Create a PHP session and use that session variable for your "IF" condition boolean.
i.e. if (session active)
{then display this object}
else
{dont display this object}
Here is some documentation on PHP 5 Sessions. PHP 5 Sessions
The neat thing about PHP is that it is completely interchangeable with HTML. Therefore you and assign elements to divs. Here is an example.
<html>
<body>
<?php
$loggedInTxt="";
$loggedOutTxt="";
if (*session*){
$loggedInTxt="<div>*some html content here*</div>"
}
else{
$loggedOutTxt="<div>*some html content here*</div>"
}
?>
<?php echo($loggedInTxt)?>
<?php echo($loggedOutTxt)?>
</body>
</html>
The idea is that you test the condition within the php and create php strings containing html elements. You can insert the anywhere in your html. So if you create a button and assign the code you used to create that button to a php variable then you can echo that variable(the code for the button) anywhere in your html script. I was blown away by the implications of php. I hope this helps simplify it! (This more of an overview. Do NOT copy and paste the code)

Logout all open tabs automatically when user logs out in one of the opened tabs in php

I am working on php session concept in php. created login page using jquery and php and created sessions for all pages when i logged in session runs i can open logged in urls in another tabs to which works great but i have an issue in logout.
when i logout in one of the opened browser tab other tabs still it runs manually if i refresh pages gets logged out. My requirement is when i logout in one of the tab other tabs should automatically logout instead of manually.
DB file
<?php
session_start();
$con = mysqli_connect("localhost", "root", "","testing") or die ("Oops! Server not connected"); // Connect to the host
?>
Login.php
<?php
include 'db.php';
if(isset($_SESSION['username']) && $_SESSION['username'] != '')
{ // Redirect to secured user page if user logged in
echo '<script type="text/javascript">window.location = "userpage.php"; </script>';
}
?>
<html>
<body>
<form>
<table class="mytable">
<tr>
<td>Username</td>
<td>
<input type="text" name="username" id="username" class="as_input" value="s"/>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" id="password" class="as_input" value="s"/>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="login" id="login" class="as_button" value="Login »" />
</td>
</tr>
</table>
</form>
</body>
</html>
welcome home page
<?php
include 'db.php';
if(!isset($_SESSION['username']) || $_SESSION['username'] == '')
{
echo '<script type="text/javascript">window.location = "login.php"; </script>';
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="as_wrapper">
<h2>
welcome to home page
</h2>
logout<br><br>
<a href='#'>test link</a>
</div>
</body>
</html>
logout.php
<?php
include 'library.php';
session_destroy();
unset($_SESSION['username']);
unset($_SESSION['password']);
echo '<script type="text/javascript">window.location = "login.php"; </script>';
?>
Create a php page:
checkStatus.php
<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] != '')
echo true;
else
echo false;
?>
Now in every page have this jQuery code:
var _delay = 3000;
function checkLoginStatus(){
$.get("checkStatus.php", function(data){
if(!data) {
window.location = "logout.php";
}
setTimeout(function(){ checkLoginStatus(); }, _delay);
});
}
checkLoginStatus();
So every page after certain ammount of delay will call a js function repeatatively a which will check the login status by making an ajax call to a php file (you have created). If the user is logged out from any it will destroy the session in the browser and make all the tabs to redirect to the logout.php page.
You need to have a javascript listener that checks if the session has been destroyed;
window.setInterval(function(){
/// call your function here to cechk the server
}, 5000);
You can use ajax to check if the user's session is still set.
You should make a call to the bellow js function after you have included ajax
var targetURL="login.php";
function auto_check_login(){
$.ajax({
url: "check_user_session.php",
cache: false,
success: function(data){
if(data != 1){
window.location=targetURL; //Redirect user to login page.
}
}
});
}
$(document).ready(function(){
auto_check_login(); //Call auto_check_login function when DOM is Ready
});
//Call auto_check_login after 2 seconds
setInterval(auto_check_login,2000);
Then in check_user_session.php file, you can have this
session_start();
if( !isset($_SESSION['username']) || !isset($_SESSION['password']) ){
print 0;
} else {
print 1;
}
You have to check if $_SESSION['username'] is set not just one time, but many times.
Check if this index exists and, if not, redirect the user to the login page.

Error: Permission denied to access property '$'

everybody.
I have the following situation:
I have:
http://example.com/ and http://example.com/new
In example.com, I have some forms that I load in example.com/new domain with fancybox iframe.
My form, basically shows some fields for the user to enter his pessoal data, like name, phone and etc... After he submit that, I show some user agreement terms that comes from database and a checkbox for the user to say that he agree with the terms.
After he check and submit, I want to alert some sucess message and the fancybox modal/iframe to close and thats it.
In the form page, i've loaded jquery, and bootstrap. So, when the user agree, I print:
<?php
echo "
<script>
alert('Some success message!');
$(document).ready(function(){
parent.$.fancybox.close();
});
</script>
";
?>
I have three forms, in one, works, in the other two, i get:
Error: Permission denied to access property '$'
The only difference between the form that works and the other two, is that in the form that works, i don't have the agreement terms coming from database, only the checkbox.
I could put my entire code here, but would be a giant question. But if you guys need, I can update.
Sorry for my english and forgive-me if I was not clear.
UPDATE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php
/* Connect with DB */
require_once('require/conectar.php');
if(!empty($_POST))
foreach($_POST as $k => $v)
$$k = $v;
?>
<script type="text/javascript" src="http://example.com/new/assets/js/jquery.js"></script>
</head>
<body>
<?php if(!isset($agree) and !isset($next)): ?>
<h1>The form</h1>
<form method="post" action="">
<label>Your name:</label>
<input type="text" name="name">
<br>
<label>Your email:</label>
<input type="text" name="email">
<br>
<input type="submit" name="next">
</form>
<?php
else:
$error = (!isset($name)) ? true : false;
$error = (!isset($name)) ? true : false;
if($error)
{
echo '<script>You must fill all fields before submit.</script>';
exit;
}
$qrr = mysql_query("SELECT * FROM `terms`");
$terms = mysql_fetch_object($qrr);
?>
<h1>Terms:</h1>
<?php echo $terms->content; ?>
<form method="post" action="">
<input type="hidden" value="<?php echo $name; ?>" name="name">
<input type="hidden" value="<?php echo $email; ?>" name="email">
<input type="checkbox" value="1" name="accept"> I agree.
<input type="submit" name="agree">
</form>
<?php
endif;
if(isset($agree))
{
/*
Here i mail me the user data.
*/
echo "
<script>
alert('Soliciação Realizada com sucesso!');
$(document).ready(function(){
parent.$.fancybox.close();
});
</script>
";
}else
{
echo "<script>alert('You need to agree with the terms to proceed.');</script>";
}
?>
</body>
</html>
This is a browser security thing. While there's a few ways around it, the best one is probably to use the postMessage API.
On your example.com parent page, add some code like this:
function handleMessageFromiFrame(event) {
alert('Some success message: ' + event.data);
//$.fancybox.close();
}
window.addEventListener("message", handleMessageFromiFrame, false);
And, then on your child example.com/new iframe, add code like this:
var parentOrigin = "*"; // set to http://example.com/ or whatever for added security.
function sendMessageToParent(){
parent.postMessage("button clicked", parentOrigin);
}
$('#submit-btn').click(sendMessageToParent);
Here's an example of it in action:
Parent example.com page: http://jsbin.com/hiqoyevici/1/edit?html,js,output
Child example.com/new iframe: http://jsbin.com/goferunudo/1/edit?html,js,output
When you click the button in the child page, it uses postMessage to notify the parent. Then the parent listens for the message and does whatever action you want.

Categories

Resources