PHP函数变量未被拾取

时间:2015-02-17 23:18:34

标签: php function variables

目标:在第二页上从Textarea(第一页方法= POST)获取数据;变量=来自第一页的连接数据,函数将变量(HTML代码)写入Excel工作表/唯一列。

问题:变量$ bike,$ moto,$ snow,$ fishing,$ outdoor,$ watersports;没有在函数CreatesumCSV()中拾取。创建的CSV不会在指定的列中显示变量值,在输出的CSV中的函数内创建的其他变量也会显示。

任何人都可以看到问题所在,请指教

<?PHP
// Second page
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

// Data from first page - TABLE HTML CODE
$bike_raw = $_POST['bike_HTML'];
$snow_raw = $_POST['snow_HTML'];
$moto_raw = $_POST['moto_HTML'];
$watersports_raw = $_POST['watersports_HTML'];
$fishing_raw = $_POST['fishing_HTML'];
$outdoor_raw = $_POST['outdoor_HTML'];

// Variables to use in function CreatesumCSV
$bike = $bike_raw.$moto_raw.$outdoor_raw.$snow_raw.$watersports_raw.$fishing_raw;
$moto = $moto_raw.$bike_raw.$outdoor_raw.$snow_raw.$watersports_raw.$fishing_raw;
$snow = $snow_raw.$bike_raw.$moto_raw.$outdoor_raw. $watersports_raw.$fishing_raw;
$fishing = $fishing_raw.$bike_raw.$moto_raw.$outdoor_raw.$snow_raw.$watersports_raw;
$outdoor = $outdoor_raw.$bike_raw.$moto_raw.$snow_raw.$watersports_raw.$fishing_raw;
$watersports = $watersports_raw.$bike_raw.$moto_raw.$outdoor_raw.$snow_raw.$fishing_raw;
$bike_def = $bike;

// Function to action CSV file creation
function CreatesumCSV() {

$Tag = "Tag"; 
$Priority = "Priority"; 
$Description = "Description"; 
$Field = "Field"; 
$Value = "Value"; 
$H = "H"; 
$A = "A"; 
$T = "T"; 

// Variables flagged as global - not being picked up
global $bike,$moto,$snow,$fishing,$outdoor,$watersports;

// CSV data array - $bike, $snow... produces empty columns. $Tag ,$Priority works fine
$data = array(
"$Tag,$Priority,$Description,$Field,$Value,$H,$A,$T",
"d_preference,,Bike,TORP7_PREF,Bike,$bike,$bike,$bike",
"d_preference,,Moto,TORP7_PREF,Moto,$moto,$moto,$moto",
"d_preference,,Snow,TORP7_PREF,Snow,$snow,$snow,$snow",
"d_preference,,Outdoor,TORP7_PREF,Outdoor,$outdoor,$outdoor,$outdoor",
"d_preference,,Fishing,TORP7_PREF,Fishing,$fishing,$fishing,$fishing",
"d_preference,,Watersports,TORP7_PREF,Watersports, $watersports,$watersports,$watersports",
"d_preference,,Default,,%%DEFAULT%%,$bike_def,$bike_def,$bike_def"
);

header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="firstsample.csv"');

// Write to CSV file
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
exit;
}

// calling CSV function
if (isset($_GET['hello'])) {
CreatesumCSV();
} 
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Create CSV</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/shopping_style.css" />
</head>
<body>
<a href='two_csv.php?hello=true'>Create CSV File</a>
</body>
</html>




<?PHP
// first page
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/shopping_style.css" />
</head>
<body>
<label>Input HTML Tables</label><br />
<textarea rows="4" cols="80" name="text">
<table width="650" cellpadding="0" cellspacing="0" border="0" id="bike"><tr><td>bike</td></tr></table>  
<table width="650" cellpadding="0" cellspacing="0" border="0" id="snow"><tr><td>snow</td></tr></table>
<table width="650" cellpadding="0" cellspacing="0" border="0" id="moto"><tr><td>moto</td></tr></table>
<table width="650" cellpadding="0" cellspacing="0" border="0" id="outdoor"><tr><td>outdoor</td></tr></table>
<table width="650" cellpadding="0" cellspacing="0" border="0" id="fishing"><tr><td>fishing</td></tr></table>
<table width="650" cellpadding="0" cellspacing="0" border="0" id="watersports"><tr><td>watersports</td></tr></table>
</textarea>
<input type="button" id="mybutton" value="ADD CONTENT">
<div id="all_HTML_STUFF" style="display:none;"></div>
<form method="POST" action="two_csv.php">
<input type="submit" name="submit" value="Send" id="submit"/>
Snow
<textarea name="snow_HTML" id="snow_HTML"></textarea>
moto
<textarea name="moto_HTML" id="moto_HTML"></textarea>
outdoor
<textarea name="outdoor_HTML" id="outdoor_HTML"></textarea>
Bike
<textarea name="bike_HTML" id="bike_HTML"></textarea>
Watersports
<textarea name="watersports_HTML" id="watersports_HTML"></textarea>
Fishing
<textarea name="fishing_HTML" id="fishing_HTML"></textarea>
Street
<textarea name="street_HTML" id="street_HTML"></textarea>
</form>
<script type="text/javascript">
$('input#mybutton').click(function() {
var all_html = $('textarea[name=text]').val();  
$("div#all_HTML_STUFF").html(all_html); 
var watersports_stuff = $("div#all_HTML_STUFF > table#watersports").prop('outerHTML');
$("textarea#watersports_HTML").val(watersports_stuff);
var bike_stuff = $("div#all_HTML_STUFF > table#bike").prop('outerHTML');
$("textarea#bike_HTML").val(bike_stuff);
var snow_stuff = $("div#all_HTML_STUFF > table#snow").prop('outerHTML');
$("textarea#snow_HTML").val(snow_stuff);
var moto_stuff = $("div#all_HTML_STUFF > table#moto").prop('outerHTML');
$("textarea#moto_HTML").val(moto_stuff);
var outdoor_stuff = $("div#all_HTML_STUFF > table#outdoor").prop('outerHTML');
$("textarea#outdoor_HTML").val(outdoor_stuff);
var fishing_stuff = $("div#all_HTML_STUFF > table#fishing").prop('outerHTML');
$("textarea#fishing_HTML").val(fishing_stuff);
});
</script>
</body>
</html>

0 个答案:

没有答案