var_dump($x);
echo strlen("gogo");
echo str_word_count("go go");
echo strrev("car");
echo strpos("car","ca");
echo str_replace("dog", "cat", "dog is bad");
var_dump(is_int($x));
$x = "Hello";
var_dump(is_numeric($x));
\$y = "555";
$int_cast = (int)$y;
echo(pi());
echo(max(0,2,4)."<br>");\
echo(abs(-1));
echo(sqrt(81));
echo(round(0.5));
echo(rand());
echo(rand(10, 100));
define("go","kk");
echo go;
define("go", "kk", true);
echo GO;
define("cat",["1","2","3"]);
echo cat[1];
echo $x + $y;
echo $x - $y;
echo $x ** $y;
$x += 1
00;
var_dump($x == $y);
var_dump($x === $y);
!=
<>
echo ++$x;
echo $x++;
and &&
or ||
if ($x == 100 || $y == 80){
echo "hi";
}
echo $txt1 . $txt2;
$txt1 .= $txt2;
echo $status = (empty($user)) ? "anoymous" : "logged in";
echo $color2 = $color2 ?? "blue";
$today = date("Y-m-d H:i:s");
echo $today;
if ($t < "10") {
} elseif ($t < "20") {
} else {
}
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
while($y <=5){
echo "$y <br>";
$y++;
}
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
<span class="error"><?php echo $websiteErr;?></span>
<style>
.error {color: #FF0000;}
</style>
一頁完成檢核
https://tryphp.w3schools.com/showphp.php?filename=demo_form_validation_special
echo "Today is ".date("Y/m/d")."<br>";
echo "The time is ".date("h:i:sa");
<?php include 'footer.php';?>
echo readfile("webdictionary.txt");
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
setcookie("test_cookie", "test", time() + 3600, '/');
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
function my_callback($item) {
return strlen($item);
}
$strings = ["apple", "orange", "banana", "coconut"];
$lengths = array_map("my_callback", $strings);
print_r($lengths);
$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);
echo json_encode($age);
return "{$prefix} {$name}";
https://www.w3schools.com/php/php_mysql_connect.asp
mysql 連結方式
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// begin the transaction
$conn->beginTransaction();
// our SQL statements
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')");
// commit the transaction
$conn->commit();
echo "New records created successfully";
} catch(PDOException $e) {
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
mysql與mysqli的區別
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
撈資料去show
https://www.w3schools.com/php/php_mysql_select.asp
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str, true);
xmlhttp.send();
}
}
</script>'
https://www.w3schools.com/php/php_ajax_database.asp
==> show table正解<?php
$q = intval($_GET['q']);$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
-------------------------------------------------------
XAMPP
https://www.slideshare.net/sshiouwu/02-xampp
<a href="url">link text</a>
==> php login 範例
download notepad++
PHP examples (example source code) Organized by topic
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
-----------------------
<script>
var names = $("select[data-identity=94] option").clone();
var names2 = $("select[data-identity=106] option").clone();
var names3 = $("select[data-identity=71] option").clone();
$('select[data-identity=70]').change(function() {
var val3 = $("select[data-identity=70] option:selected").text();
if( val3.indexOf("六")<0 && val3.indexOf("星期日")<0 ){
$("select[data-identity=71]").empty();
names3.filter(function(idx, el) {
return $(el).text().indexOf('時段一') < 0 ;
}).appendTo("select[data-identity=71]");
}else{
$("select[data-identity=71]").empty();
names3.filter(function(idx, el) {
return $(el).text().indexOf('全部') < 0 ;
}).appendTo("select[data-identity=71]");
}
});
$('ul[data-identity=89]').children().change(function() {
$("select[data-identity=94]").empty();
$("select[data-identity=106]").empty();
$("input[name=Column029]").attr("checked",false);
$("input[name=Column024]").attr("checked",false);
$("input[name=Column028]").attr("checked",false);
});
$('input[data-identity=96]').change(function() {
if((Number($('input[data-identity=96]').val()) + Number($('input[data-identity=97]').val())) > Number($('input[data-identity=92]').val()) ){
$('input[data-identity=96]').after("<span style='color:red' class='Err'>葷食+素食人數不可以大於參訪人數!</span>");
}else{
$('.Err').attr("hidden","hidden");
}
});
$('input[data-identity=97]').change(function() {
if((Number($('input[data-identity=96]').val()) + Number($('input[data-identity=97]').val())) > Number($('input[data-identity=92]').val()) ){
$("span.Err").remove();
$('input[data-identity=97]').after("<span style='color:red' class='Err'>葷食+素食人數不可以大於參訪人數!</span>");
}else{
$('.Err').attr("hidden","hidden");
}
});
$('input[data-identity=92]').change(function() {
if( Number($('input[data-identity=92]').val()) >50 ){
$("span.Err2").remove();
$('input[data-identity=92]').after("<span style='color:blue' class='Err2'>提醒:參訪人數上限150人,另最大貴賓室可容納人數為50人</span>");
}else{
$('.Err2').attr("hidden","hidden");
}
});
$('input[data-identity=93]').change(function() {
if(Number($('input[data-identity=93]').val()) > Number($('input[data-identity=92]').val()) ){
$("span.Err3").remove();
$('input[data-identity=93]').after("<span style='color:red' class='Err3'>不可以大於參訪人數!</span>");
}else{
$('.Err3').attr("hidden","hidden");
}
});
$('#ApplyCellphone').change(function() {
if ($('#ApplyTelephone').val() == "")
alert("電話也要填");
});
$('#ApplyTelephone').change(function() {
if ($('#ApplyCellphone').val() == "")
alert("手機也要填");
});
$('input[data-identity=6],input[data-identity=8],input[data-identity=28],input[data-identity=29],input[data-identity=7],input[data-identity=11],input[data-identity=13],input[data-identity=14],input[data-identity=25],input[data-identity=26],input[data-identity=27],input[data-identity=131],input[data-identity=51],input[data-identity=41],input[data-identity=154]').mouseover(function(){
$('textarea[data-identity=150]').val('');
});
$('select[data-identity=70]').change(function() {
var val= $("select[data-identity=70] option:selected").text();
var Today = new Date();
if(Today.getMonth()+1 == 2){
var temp = val.substring( val.indexOf("月")+1 , val.indexOf("日"));
if (temp - Today.getDate() <2){
alert("不得小於2日內預約");
}
}
});
$('select[data-identity=71] , select[data-identity=70], input[type=radio][data-identity=163]').change(function() {
var val= $("select[data-identity=70] option:selected").text();
val = val.substring( 0 , val.indexOf("日")+1);
var val2= $("select[data-identity=71] option:selected").text();
val2 = val2.substring( val2.indexOf("時"), (val2.indexOf("時")+3));
if (val2 == "請選"){ val2 = "";}
var val4 ="貴賓簡報室";
//if ($('input[type=radio][data-identity=163]:checked').next('label:first').html()=="否"){
//val4 ="B";
//}
$("select[data-identity=94]").empty();
names.filter(function(idx, el) {
return $(el).text().indexOf(val +')'+ val2) >= 0 && $(el).text().indexOf(val4) >= 0 ;
}).appendTo("select[data-identity=94]");
var val4= $("select[data-identity=71] option:selected").text();
val4 = val4.substring( val4.indexOf("段")+1, (val4.indexOf("段")+2));
name2 = names2.filter(function(idx, el) {
return $(el).text().indexOf(val4) > 0;
});
var val3 = $("select[data-identity=70] option:selected").text();
if( val3.indexOf("五")<0 && val3.indexOf("六")<0 && val3.indexOf("星期日")<0 ){
$("select[data-identity=106]").empty();
names2.filter(function(idx, el) {
return $(el).text().indexOf('12:00') < 0 && $(el).text().indexOf(val4) > 0;
}).appendTo("select[data-identity=106]");
}else{
$("select[data-identity=106]").empty();
names2.filter(function(idx, el) {
return $(el).text().indexOf(':') > 0 && $(el).text().indexOf(val4) > 0;
}).appendTo("select[data-identity=106]");
}
});
$(function(){
$('div.ps.formHrard').css("color", "red");
});
</script>
-----------------------
<script>
$('textarea[name=Column015]').attr('disabled',true);
$('textarea[name=Column016]').attr('disabled',true);
$('textarea[name=Column017]').attr('disabled',true);
$('textarea[name=Column018]').attr('disabled',true);
$('input[type=radio][name=Column006]').change(function() {
if($('input[type=radio][name=Column006]:checked').next('label:first').html()=="否" ){
$('div[data-identity=13]').after("<span style='color:red' class='Err'>非居住/戶籍/工作於臺中市,無法繼續報名!</span>");
$('#BtnSend').attr('disabled',true);
}else{
$('.Err').attr("hidden","hidden");
$('#BtnSend').attr('disabled',false);
}
});
var names1 = $("select[data-identity=29] option").clone();
var names2 = $("select[data-identity=30] option").clone();
var names3 = $("select[data-identity=31] option").clone();
var names4 = $("select[data-identity=32] option").clone();
//names1.push('<option value=\"\">抱歉,您所勾選的行政區目前無開課</option>');
//alert('test');
var $checks2 = $(':checkbox[dynamicFormColumn_name="Column009"]');
$checks2.change(function() {
//$('option:selected').prop('selected', false);
$('textarea').val('');
/*
$('textarea[data-identity=25').val('');
$('textarea[data-identity=33]').val('');
$('textarea[data-identity=34]').val('');
$('textarea[data-identity=35]').val('');
$("select[data-identity=29] option").prop('selected', false);
$("select[data-identity=30] option").prop('selected', false);
$("select[data-identity=31] option").prop('selected', false);
$("select[data-identity=32] option").prop('selected', false);
*/
$("select[data-identity=29]").val('');
$("select[data-identity=30]").val('');
$("select[data-identity=31]").val('');
$("select[data-identity=32]").val('');
var $checked2 = $checks2.filter(':checked');
if ($checked2.length>2) {
$checks2.attr("checked",false);
$checks2.prop("checked",false);
}
});
var $checks = $(':checkbox[dynamicFormColumn_name="Column010"]');
$checks.change(function() {
var $checked = $checks.filter(':checked');
if (!$checked.length) {
$("select[data-identity=29]").empty();
names1.appendTo("select[data-identity=29]");
$("select[data-identity=29]")[0].selectedIndex = 0;
$("select[data-identity=30]").empty();
names2.appendTo("select[data-identity=30]");
$("select[data-identity=30]")[0].selectedIndex = 0;
$("select[data-identity=31]").empty();
names3.appendTo("select[data-identity=31]");
$("select[data-identity=31]")[0].selectedIndex = 0;
$("select[data-identity=32]").empty();
names4.appendTo("select[data-identity=32]");
$("select[data-identity=32]")[0].selectedIndex = 0;
return;
}
var filters = $checked.map(function() {
return this.id
});
//alert($("label[for='"+filters[1]+"']").text());
$("select[data-identity=29]").empty();
$("select[data-identity=30]").empty();
$("select[data-identity=31]").empty();
$("select[data-identity=32]").empty();
$('textarea').val('');
for(var i =0; i< filters.length; i++){
var temp = $("label[for='"+filters[i]+"']").text();
names1.filter(function(idx, el) {
return $(el).text().indexOf(temp) >= 0 || $(el).text() === '請選擇' ;
}).appendTo("select[data-identity=29]");
names2.filter(function(idx, el) {
return $(el).text().indexOf(temp) >= 0 || $(el).text() === '請選擇' ;
}).appendTo("select[data-identity=30]");
names3.filter(function(idx, el) {
return $(el).text().indexOf(temp) >= 0 || $(el).text() === '請選擇' ;
}).appendTo("select[data-identity=31]");
names4.filter(function(idx, el) {
return $(el).text().indexOf(temp) >= 0 || $(el).text() === '請選擇' ;
}).appendTo("select[data-identity=32]");
}
$("select[data-identity=29]").val('');
$("select[data-identity=30]").val('');
$("select[data-identity=31]").val('');
$("select[data-identity=32]").val('');
if($("select[data-identity=29]").children('option').length == 1){
$("select[data-identity=29]").empty();
$("select[data-identity=29]").append($("<option></option>").attr("value","").text("抱歉,您所選擇的行政區目前沒有開課"));
}
if($("select[data-identity=30]").children('option').length == 1){
$("select[data-identity=30]").empty();
$("select[data-identity=30]").append($("<option></option>").attr("value","").text("抱歉,您所選擇的行政區目前沒有開課"));
}
if($("select[data-identity=31]").children('option').length == 1){
$("select[data-identity=31]").empty();
$("select[data-identity=31]").append($("<option></option>").attr("value","").text("抱歉,您所選擇的行政區目前沒有開課"));
}
if($("select[data-identity=32]").children('option').length == 1){
$("select[data-identity=32]").empty();
$("select[data-identity=32]").append($("<option></option>").attr("value","").text("抱歉,您所選擇的行政區目前沒有開課"));
}
});
</script>
<script>
$('input[data-identity=5], input[data-identity=6], #RseTime').change(function() {
if($('input[data-identity=5]').val() !=""){
if(validateTime($('input[data-identity=5]').val()))
{
$("span.Err").remove();
$('input[data-identity=5]').after("<span style='color:red' class='Err'>格式不對!正確如12:20</span>");
$('#BtnSend').attr('disabled',true);
}else{
$('.Err').attr("hidden","hidden");
$('#BtnSend').attr('disabled',false);
}
if($('#RseTime').val() !=""){
if(validate1hr($('input[data-identity=5]').val()))
{
$("span.Err3").remove();
$('input[data-identity=5]').after("<span style='color:red' class='Err3'>應在預約時間前1小時範圍內!</span>");
$('#BtnSend').attr('disabled',true);
}else{
$('.Err3').attr("hidden","hidden");
$('#BtnSend').attr('disabled',false);
}
}
}
if($('input[data-identity=6]').val() !=""){
if(validateTime($('input[data-identity=6]').val()))
{
$("span.Err2").remove();
$('input[data-identity=6]').after("<span style='color:red' class='Err2'>格式不對!正確如12:20</span>");
$('#BtnSend').attr('disabled',true);
}else{
$('.Err2').attr("hidden","hidden");
$('#BtnSend').attr('disabled',false);
}
if(validateRange($('input[data-identity=6]').val()))
{
$("span.Err4").remove();
$('input[data-identity=6]').after("<span style='color:red' class='Err4'>應在預計到達時間+1小時範圍內!</span>");
$('#BtnSend').attr('disabled',true);
}else{
$('.Err4').attr("hidden","hidden");
$('#BtnSend').attr('disabled',false);
}
}
/*
if($('#RseCount').val() !=""){
if(validateNumber($('#RseCount').val()))
{
$("span.Err5").remove();
$('#RseCount').after("<span style='color:red' class='Err5'>參訪預約人數只能輸入介於25~30人!</span>");
}else{
$('.Err5').attr("hidden","hidden");
}
}
*/
});
$('#RseTime').change(function() {
var reservationTime =$('#RseTime').val().split(" ");
if (reservationTime[1] == "09:00"){
$('input[type=radio][name=Column002][value=161587]').attr('checked', 'checked');
$('input[type=radio][name=Column002][value=161587]').next().css("color", "red");
}else if(reservationTime[1] == "10:00"){
$('input[type=radio][name=Column002][value=161588]').attr('checked', 'checked');
$('input[type=radio][name=Column002][value=161588]').next().css("color", "red");
}else if(reservationTime[1] == "13:00"){
$('input[type=radio][name=Column002][value=161589]').attr('checked', 'checked');
$('input[type=radio][name=Column002][value=161589]').next().css("color", "red");
}else if(reservationTime[1] == "15:00"){
$('input[type=radio][name=Column002][value=161590]').attr('checked', 'checked');
$('input[type=radio][name=Column002][value=161590]').next().css("color", "red");
}
//$('input[type=radio][name=Column002]').attr('disabled', 'disabled');
});
function validateTime(value) {
value = new String(value);
var isValid = false;
var a = value.match(/^(\d{0,2}):(\d{0,2})$/);
if (a == null) return true;
if ( a[0]>=24 || a[1]>=60) return true;
return isValid;
}
function validate1hr(value) {
value = new String(value);
var isValid = false;
var timeBeforeArray = value.split(":");
var reservationTime = ($('#RseTime').val()).split(" ");
if (reservationTime[1] == "09:00"){
if(parseInt(timeBeforeArray[0]) < 8 || parseInt(timeBeforeArray[0]) > 9){
isValid = true;
}
}else if(reservationTime[1] == "10:00"){
if(parseInt(timeBeforeArray[0]) < 9 || parseInt(timeBeforeArray[0]) > 11){
isValid = true;
}
}else if(reservationTime[1] == "13:00"){
if(parseInt(timeBeforeArray[0]) < 12 || parseInt(timeBeforeArray[0]) > 14){
isValid = true;
}
}else if(reservationTime[1] == "15:00"){
if(parseInt(timeBeforeArray[0]) < 14 || parseInt(timeBeforeArray[0]) > 15){
isValid = true;
}
}
return isValid;
}
function validateRange(value) {
value = new String(value);
var isValid = false;
var beforeTime = $('input[data-identity=5]').val();
var timeBeforeArray = beforeTime.split(":");
var timeAfterArray = value.split(":");
if(parseInt(timeAfterArray[0])>parseInt(timeBeforeArray[0])+1){
isValid = true;
}else if(parseInt(timeAfterArray[0]) == parseInt(timeBeforeArray[0])+1 && parseInt(timeAfterArray[1])>parseInt(timeBeforeArray[1])){
isValid = true;
}
else if(parseInt(timeAfterArray[0])<parseInt(timeBeforeArray[0])){
isValid = true;
}else if(parseInt(timeAfterArray[0]) == parseInt(timeBeforeArray[0]) && parseInt(timeAfterArray[1])<parseInt(timeBeforeArray[1])){
isValid = true;
}
return isValid;
}
function validateNumber(value) {
var isValid = false;
if ( parseInt(value) < 25 || parseInt(value) >30) {
isValid = true;
}
return isValid;
}
</script>
---------------------------
table, th, td {
border: 1px solid black;
}