i try to solve a problem but didnt succeed. I retrieve datas from google analytics and i have to send start and end date. I have two textbox and user select start and end date.However, but before user select start and end date. The problem occurs..
The place that i use isset is here,
This is my full html
<?php
define('ga_email','mertmetinbjk#gmail.com');
define('ga_password','************');
define('ga_profile_id','*******');
require 'gapi.class.php';
require 'connectDB.php';
$ga = new gapi(ga_email,ga_password);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>İçerik</title>
<script language="javascript" type="text/javascript" src="datetimepicker.js">
//Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
//Script featured on JavaScript Kit (http://www.javascriptkit.com)
//For this script, visit http://www.javascriptkit.com
</script>
</head>
<div></div>
<body>
<table align="center">
<tr>
<td><br />
<br />
<form action="#" method="post" name="formDates" id="form1" >
<table width="303" height="40" border="0" align="center">
<table width =""
<tr>
<td>Başlangıç Tarihi</td>
<td>
<label for="txtStartDate"></label>
<input type="text" name="txtStartDate" id="txt1" value="" /><a href="javascript:NewCal('txt1','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Başlangıç Tarihi Seçin">
</a>
</td>
</tr>
<tr>
<td>Bitiş Tarihi</td>
<td>
<label for="txtEndDate"></label>
<input type="text" name="txtEndDate" id="txt2" /><a href="javascript:NewCal('txt2','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Bitiş Tarihi Seçin">
</a>
</td>
</tr>
</form>
<tr>
<td><input type="submit" name="btnSubmit" value="Gönder"</td>
</tr>
</table>
<table width="1250" height="26" align="center">
<tr>
<td width="138"><strong>Kanal Adı</strong>
<td width="150"><b>Kategori Adı</b></td>
<td width="130"><p><strong>Event Adı</strong></td>
<td width="145"><strong>Toplam</strong>
</tr>
</table>
<?php
if(isset($_POST['submit'])){
$startDate = $_POST['txtStartDate'];
$endDate = $_POST['txtEndDate'];
}
$arrDates = explode("-",$startDate);
$startDate=$arrDates[2]."-".$arrDates[1]."-".$arrDates[0];
$arrDates = explode("-",$endDate);
$endDate = $arrDates[2]."-".$arrDates[1]."-".$arrDates[0];
echo $startDate;
echo $endDate;
$ga->requestReportData(ga_profile_id,array('eventCategory','eventAction'),array('totalEvents'),$sort_metric=null,$filter='eventAction==InitPlayer',$start_date='2012-02-02',$end_date='2012-02-16');
foreach($ga->getResults() as $result2);
{
echo $result2->geteventCategory();
echo $result2->geteventAction();
echo $result2->gettotalEvents();
}
$ga->requestAccountData();
$mysql = new mysql();
$mysql->connect();
$counter = 0;
foreach($ga->getResults() as $result)
{
echo $result . ' (' . $result->getProfileId() . ")<br />";
echo "<br />";
$counter++;
}
$result = mysql_query("select profile_id from profiles");
$num_rows = mysql_num_rows($result);
if($counter != $num_rows)
{
$mysql->query("delete from profiles");
foreach($ga->getResults() as $result)
{
$mysql->query("insert into profiles values(".$result->getProfileId().",'".$result."')");
}
}
?>
</td>
</tr>
</table>
</body>
</head>
</html>
what is wrong?
if a choose start and end date problems is solved , problems occurs before that i choose
It must be isset($_POST["btnSubmit"]) NOT submit
submit = type
btnSubmit = name of the button
I think it should be:
if(isset($_POST['submit']))
instead of:
if(!isset($_POST['submit']))
The exclamation mark in front of isset means not. Also I would check for the actual fields you want to use, not for $_POST['submit']:
if(isset($_POST['txtStartDate']) && isset($_POST['txtEndDate']))
{
// Do something with $_POST['txtStartDate'] and $_POST['txtEndDate']
}
The reason you were getting the error is that $_POST['submit'] does not exist. There is not input element with a name attribute called submit. Beware of the difference between type and name.
Undefined index txtStartDate, ...
It seems that your form may not be conforming to the same variable name specification with the one that you use in this php file. Make sure field names are matching within the form and here.
And also you should make these checks in your php file after submission success: if(!isset($_POST['submit']))
When your page is loading and $_POST['submit'] is not set, your $_POST['txtStartDate'] and $_POST['txtEndDate'] are not set so $arrDates is not set too. You need to check if these $_POST vars are set or not, to work with them.
You have to use isset() on every variable, not just one variable. $_POST['txtStartDate'] and $_POST['txtEndDate'] and other variables are simply not defined, this means that they were not sent with the POST request.
I guess it should be if(isset($_POST['submit'])) instead of if(!isset($_POST['submit'])) as ! in the start means a negation
if(!isset($_POST['submit']))
This means that if the submit button was NOT pressed. You should check if the $_POST['submit'] is set, as a result of the form submission, like so:
if(isset($_POST['submit']))
Related
I am supposed to have my form elements repopulate using php and html only after I click the link from page two. However, none of the fields are repopulating. Please help me!!?
Page 1:
<?php
session_start();
?>
<?php
/*
This page should:
1) Use a cookie (NOT PHP SESSION) that expires a year from now to do the following:
Record the timestamp of the FIRST load of this page (but NOT any subsequent load of this page).
Note: You only need first 3 parameters in setcookie() for this HW.
2) If the suvey was already completed, transfer to third page (thank you page).
That page instructs you to set a cookie that will allow you to detect this.
Recall from the CRUD example how page transfers are done in PHP:
header("Location: URL");
3) The form in this page should submit to THIS PAGE, and then transfer to hw_page2.php
after saving stuff to PHPs built-in $_SESSION superglobal.
The data will then be available in all the other pages (remember no database is allowed for this HW).
4) If the button in the 2nd page is clicked to come back to this page, the
Session data should re-populate into the form.
*/
if (!isset($_COOKIE['load_date'])) {
setcookie('load_date', time(), time() + (86400 * 365));
}
?>
<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h3><?=$message?></h3>
User Profile:
<?php
$load_date = $_COOKIE['load_date'];
$_SESSION['is_cool'] = $_POST['is_cool'];
$_SESSION['like_bands'] = $_POST['like_bands'];
$_SESSION['other_band'] = $_POST['other_band'];
$is_cool = $_SESSION['is_cool'];
$bands = $_SESSION['like_bands'];
$other_band = $_SESSION['other_band'];
print_r($bands);
echo $other_band;
echo $is_cool;
?>
<br><br>
<form action="hw_page1.php" method="POST" name="form1" onsubmit="return validate_form()">
<input type="hidden" name="task" value="process_profile">
<input type="checkbox" name="is_cool" value= "yes" <?php if ($is_cool == 'yes') {
echo 'checked = "yes"';
}?>>
Are you cool?
<br><br>
What Bands do you like?
<br>
<select name="like_bands[]" multiple> <small>* Required Field</small>
<option value="Sabbath" <?php if (isset($_SESSION['like_bands']) && in_array("Mastodon", $bands)) {
echo 'selected';
} ?>> Black Sabbath</option>
<option value="Mastodon" <?php if (isset($_SESSION['like_bands']) && in_array("Mastodon", $bands)) {
echo 'selected';
} ?> >Mastodon</option>
<option value="Metallica" <?php if (isset($_SESSION['like_bands']) && in_array("Metallica", $bands)) {
echo 'selected';
} ?> >Metallica</option>
<option value="Swift" <?php if (isset($_SESSION['like_bands']) && in_array("Swift", $bands)) {
echo 'selected';
} ?> >Taylor Swift</option>
</select>
<br><br>
Favorite band not in the above list.
<br>
<input type="text" name="other_band" value="<?=$other_band?>">
<br><br>
<button type="submit" name= 'submit' value = 'submit'> Continue/Confirm </button>
</form>
<script>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Client-side form validation
// Don't change the names of stuff in the form, and you won't have to change anything below
// Used built-in JS instead of JQuery or other validation tool
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_form() {
var form_obj = document.form1;
var count_liked = 0;
for (var i=0 ; i < form_obj['like_bands[]'].options.length ; i++ ) {
if (form_obj['like_bands[]'].options[i].selected) {
count_liked++;
}
}
if (count_liked == 0) {
alert("You must choose a band from the menu.");
return false; // cancel form submission
}
return true; // trigger form submission
}
</script>
<?php
$_SESSION['is_cool'] = $is_cool;
$_SESSION['like_bands'] = $bands;
$_SESSION['other_band'] = $other_band;
echo $_SESSION['is_cool'];
if (isset($_SESSION['like_bands'])) {
header('Location: hw_page2.php');
}
?>
</body>
</html>
My second Page:
<?php
session_start();
/*
This page should:
1) Transfer back to hw_page1.php if loaded directly without the survey being completed (no survey data in session).
2) Display the Survey Data submitted from the previous page.
3) The form in this page should submit to THIS PAGE.
Use PHP to validate that the form below is completed.
Validate that the signature is not the empty string or only blank spaces (use PHP trim() function)
And of course validate that the checkbox was checked.
If the validation passes, save the signature into SESSION ande transfer to hw_page3.php.
If the validation fails, don't transfer.
Instead, back through to the form in this page WITH an appropriate message.
In that case, the Survey Data MUST also re-display in this page.
Note:
hw_page1.php used client-side JavaScript validation to ensure that all data was collected.
For the form below. do NOT use client-side JavaScript validation, but rather use PHP as instructed above.
Client-side validation is convenient for the end user, but can be bypassed by someone with know-how.
*/
?>
<?php
if (!isset($_SESSION['is_cool']) and !isset($_SESSION['like_bands'])) {
header('Location: hw_page1.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Verify Profile</title>
</head>
<body>
<?php
$is_cool = $_SESSION['is_cool'];
$bands = $_SESSION['like_bands'];
$other_band = $_SESSION['other_band'];
echo $is_cool;
print_r($bands);
echo $other_band;
$_SESSION['is_cool'] = $is_cool;
$_SESSION['like_bands'] = $bands;
$_SESSION['other_band'] = $other_band;
$bandslist = "";
foreach ($bands as $band) {
$bandslist .= "$band, ";
}
?>
<!-- Display Survey Data Here -->
<table width="" border="1" cellspacing="0" cellpadding="5">
<tr valign="top">
<td>Are you cool?</td>
<td>Liked Bands</td>
<td>Other Favorite Band</td>
</tr>
<tr valign="top">
<td><?= $_SESSION['is_cool']; ?></td>
<td><?= $bandslist; ?></td>
<td><?= $other_band; ?></td>
</tr>
</table>
<br><br>
<form action="hw_page2.php" method="GET" >
Verify that your profile data shown above is accurate by signing below.
<br>
<input type="text" name="signature" value="" placeholder="Sign Here">
<br>
<input type="checkbox" name="user_certify" value="yes">
I certify, under penalty of purgery, that my profile is accurate.
<br><br>
<button type="button" onclick="window.location='hw_page1.php';"> Go Back: Edit Profile Before Signing </button>
<!-- This is NOT a Submit Button! -->
<br>
<button type="submit"> Record Profile </button>
<!-- This is obviously -->
</form>
</body>
</html>
Please help me figure out where I am making a mistake! I have been working on it for days but I cannot figure out the answer.
i create a combobox with sql values but how i know what the value selected?
this is my code, have a lot of scrap but is my tests :)
I link to send the selected option to another php file already created.
<?php
require_once('auth.php');
require_once('config.php');
require_once('no-cache-headers.php');
require_once('functions.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nova Mensagem</title>
<link href="Formatacao.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Bem-vindo <?php echo $_SESSION['USERNAME'];?></h1>
<form id="regForm" name="regForm" method="post" action="verificarMensagem.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<?php
mysql_connect('localhost','comunicat','comunicat');
mysql_select_db('Comunicat');
$iduser =$_SESSION['SESS_MEMBER_ID'];
$query="Select * from Usuarios where id <> '$iduser'";
$resultado=mysql_query($query);
echo '<select name=”Nome”>';
while($linha=mysql_fetch_array($resultado))
{
echo '<option value="' . $linha['ID'] . '">' . $linha['Nome'] . '</option>';
}
echo '</select>';
?>
<textarea rows="4" cols="50" name="mensagem" id="mensagem">
</textarea>
<td> </td>
<td><input type="submit" name="Submit" value="Enviar" /></td>
</tr>
</table>
</form>
</body>
</html>
You can get value of select element in php by using $_POST[name-of-element]:
<?php
echo $_POST['Nome'];
?>
That works also with checkboxs,radios,etc
When the form that contains your "combobox" is submitted, you can get the selected value from your combobox with the line of code below:
$val = $_POST['Nome']; // if the form was submitted using post method
$val = $_GET['Nome']; // if the form was submitted using get method
NB
Do not use mysql_* no more, it is officially deprecated. Use mysqli or PDO instead.
I'm trying to load part of a targeted URL ($url) into my HTML page, currently the $url page sends back a name, model, type and self recalculate tool - all of which live in a HTML TR tags.
Once I post my daily password from password.php than my users click on $url to get their daily inventory from it, again and i don't mean to be redundant, but the $url itself is provided to me by my clients and I don't want my users to be able to see all the links on that page - Only what pertain to their job.
Also rather than me creating password.php is there a way for me to "collect" this password from my daily outlook exchange email and post it to the header of the $url??
Many thx and I hope i was descriptive enough.
<html>
<head>
<title>Post Password</title>
</head>
<body>
<?php
$password = "";
?>
<form name="password" method="post" action="users.php">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Paste the Daily Password</td>
<td><input type="text" maxlength=7 name="password"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>First Page</title>
</head>
<body>
<?php
$url_append = "?secret=";
//$psk = $password;
$linktext = "Click here to get a Pre Shared Key";
$password = $_POST["password"];
$url = "http://example.subexample.xx.com/";
$url .= $url_append;
$url .= $password;
$final_url = $url;
// end base users
?>
<!--I took this form below from the URL that my client host-->
<!--His url have way more input than all_products-->
<!-- but that's the only one that I want to echo for my employees to use -->
<form name="form1" method="post" action="<?php echo htmlspecialchars($final_url);?>">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Main Inventory</td>
<td><input type="text" maxlength=7 name="all_products"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
For sending POST through header take a look at this.
As for the omitting the parts of html, you can use alternative syntax like this
<?php if ($a == 5): ?>
<p>This will be omitted if condition is not true</p>
<?php endif; ?>
Similar can be used for for loop.
Read more on alternative syntax.
I'm trying to pass variables from the form to multiple pages in php
The form "rentcheck.php"
<?php require ("Connections/Project.php") ;?>
<?php session_start();?>
<title>Rent Check</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="rent.php">
<table width="385" height="70" border="1">
<tr>
<td><label for="select3">Select Customer</label>
<select name="Customer_ID" id="Customer_ID">
<?php
//Select from SQL Database Table (t_customer)
$sql=mysql_query("SELECT * from t_customer");
while ($Customer = mysql_fetch_array($sql)){
echo "<option value='".$Customer['Customer_ID']."'>".$Customer['Customer_Name']."</option>";
}
?>
</select></td>
</tr>
</table>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
After input and Pass it to page "rent.php"
<?php
require("Connections/Project.php");
//$_SESSION['yourvariable'] = 'foo';
//$newDate = date("d-m-Y", strtotime($row_Recordset1['Customer_CC_Exp_Date']));
session_start();
$datetoday=date("Y-m-d H:i:s");
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
if(isset($_GET['page'])){
$pages=array("products","cart");
if(in_array($_GET['page'], $pages)){
$_page=$_GET['page'];
}else{
$_page="products";
}
}else{
$_page="products";
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rent</title>
</head>
<body>
<p> <?php require($_page. ".php");?>
<?php echo $_POST['Customer_ID'];?></p>
</body>
</html>
This page (rent.php) shows the value from the form.
And the third page "products.php"
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$custget=$_SESSION['Customer_ID'];
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id][$custget])){
$_SESSION['cart'][$id]['quantity']++;
$getcust=$_SESSION['Customer_ID'];
}else{
$sql_s="SELECT * FROM t_dvd_copy
WHERE dvd_copy_id={$id}";
$query_s=mysql_query($sql_s);
if(mysql_num_rows($query_s)!=0){
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row_s['dvd_copy_id']]=array(
"quantity" =>1,
"price" => $row_s['price']
);
}else{
$message="NO";
}
}
}
?>
<?php if(isset($message)) {
echo"$message"; }
//echo print_r($_SESSION['cart']); ?>
<table width="489" height="52" border="1">
<tr>
<td width="123">DVD Copy ID</td>
<td width="120">Name</td>
<td width="91">Price</td>
<td width="127">Action</td>
</tr>
<?php
$sql="SELECT *, dvd_title FROM t_dvd_copy INNER JOIN t_dvd ORDER BY dvd_title ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['dvd_copy_id']?></td>
<td><?php echo $row['dvd_title']?></td>
<td><?php echo $row['price']?></td>
<td>Add To Cart</td>
<?php
}
?>
</table>
<body>
</body>
</html>
This page (products.php) shows:
Notice: Undefined index: Customer_ID in C:\xampp\htdocs\project3\rent.php on line 39" whenever I clicked the "Add to Cart" or manually type "rent.php?=cart".
I'm trying to do is to show(Customer_ID)/pass the variables on multiple pages("products.php","cart.php").
Any suggestions or ideas?
I think your problem is that you have not started the session on the other pages.
on every php page that you want to have a session you need to put session_start(); at the top.
if you don't the session will end and empty all the data from it.
if you want to make sure what is in your session you can print it out like so:
echo "<pre>";
echo print_r($_SESSION);
echo "</pre>";
Couple of things:
You mentioned that when you clicked "Add to Cart" it didn't work. I assume you mean the button labelled "Submit" on the rentcheck.php page. Can you confirm this is right please?
You would get an error when manually navigating to rent.php?=cart as you are looking for a Customer_ID key in the $_POST array but at this point you are not posting to the server, you are performing a $_GET request.
You have referenced $_SESSION['Customer_ID'] but I cannot see in your code where you have set this (products.php 7th line of code).
I would suggest thinking through how a user might navigate through your website:
where are they likely to start?
what variables will be available to you at this point?
what will you do if the required variables do not exist (e.g. $_SESSION)?
what is the earliest point you can collect the required information (your form)?
once the form has been completed correctly, how can I make sure that the required information is retained throughout the users session?
have I continued to test on each page that my required variables are available to me?
Start with that and see where it leads you.
Looking at the code I can see that you are including products.php inside rent.php
So a couple of thoughts here:
products.php should not have HTML <head> because it will be rendered
inside the <body>
If you have a variable available in rent.php it will be available
under the same name in products.php.
So if you have in rent.php:
$customer_id = $_POST['Customer_ID'];
require('products.php');
In products.php you can use $customer_id directly.
I need a small example on sending date to mysql db from a php form. Tried lots of examples but I seems I am missing o step.
I have prepared a form for 3 fields, name last name and birth date. When I fill the form and submit date filed gets crazy :) 0000-00-00 00:00:00
I tried to format the date but this time I could not understand how to pass it to mysql. Here is my funny code;
<?php include "header.php" ?>
<?php include "conn.php" ?>
<script>
$(function() {
$( "#bdate" ).datepicker();
});
</script>
<?php
if(isset($_POST['submit'])){
$PlayerName = $_POST['PlayerName'];
$PlayerLname = $_POST['PlayerLname'];
$PlayerBdate = $_POST['PlayerBdate'];
$query = "INSERT INTO tblPlayer (
PlayerName, PlayerLname, PlayerBdate
) VALUES (
'{$PlayerName}', '{$PlayerLname}', '{$PlayerBdate}'
)";
if(mysql_query($query,$connection)){
//Sucess
header("Location: players_list.php");
} else{
echo "<p>something is wrong</p>";
echo "<p>" . mysql_error() . "</p>";
}
}
?>
<form action="add_player.php" method="post">
<table>
<tr>
<td>Name: </td>
<td><input type="text" name="PlayerName" /></td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="PlayerLastName" /></td>
</tr>
<tr>
<td>Birthdate:</td>
<td><input id="bdate" type="text" name="PlayerBdate" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Save" /></td>
<td align="right"> </td>
</tr>
</table>
<?php ob_end_flush() ?>
<?php include ("footer.php") ?>
header.php contains
<?php ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<title>Untitled Document</title>
</head>
<body>
and conn.php is the standard connection sting file.Footer.php is just closing the connection:)
Also I could not make my code work without the ob_start(); and end functions:S
So can you point out how can I get the date as day/month/year format and save it to mysql that way?
since your date in mysql is in Y-m-d H:i:s format.. use php date() function to convert the date into required format.
replace
$PlayerBdate = $_POST['PlayerBdate'];
with
$PlayerBdate = date('Y-m-d H:i:s',strtotime($_POST['PlayerBdate']));
The smartest thing would just to make sure your datepicker returns it in the correct format,
$( "#bdate" ).datepicker({ dateFormat: "yy-mm-dd" }); will make the datepicker return it in the correct format. It will be missing the time, but mysql will automatically fill that for you.