我之前有一个HTML文件,其中包含一个与PHP文件分开的表单,该文件包含对表单执行操作的脚本 - 这完美无误地运行。
这个问题是页面将重定向到PHP脚本,而我更喜欢表单结果显示在同一页面上,以便用户可以再次“搜索”。将PHP脚本添加到HTML页面末尾并将HTML页面转换为PHP页面时,会出现以下问题:
1)错误“注意:显示未定义的索引:第16行/my_api.php中的product_sku 。
2)我添加了if
语句,它将回显文本“产品SKU xxx在数据库中不存在。”如果未设置包含结果的变量。这现在永久显示。
结果截图:
我认为这些错误都会发生,因为表单尚未提交,因此尚未设置已发布的表单数据和脚本结果。提交表单的结果对于成功和不成功的查询仍然是正确的,所以问题是我需要做什么才能在提交表单之前隐藏这两个错误?
请参阅下面的代码:
<html>
<head>
<title>My API</title>
<link rel="stylesheet" type="text/css" href=/api.css">
</head>
<body>
Enter a product SKU to get the price
<form class="get_price" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="product_sku">
<input type="submit" class="form_submit">
</form>
</body>
</html>
<?php
/* Call form Submission SKU Field */
$product_sku = $_POST['product_sku'];
/* Variables */
$callbackURL = "Edited";
$temporaryCredentialsRequestURL = "https://ts564737-container.zoeysite.com/oauth/initiate?oauth_callback=" . URLencode($callbackURL);
$adminAuthorizationURL = 'https://ts564737-container.zoeysite.com/admin/oauth_authorize';
$accessTokenRequestURL = 'https://ts564737-container.zoeysite.com/oauth/token';
$URL = 'https://ts564737-container.zoeysite.com';
$apiURL = $URL . '/api/rest';
$consumerKey = 'Edited';
$consumerSecret = 'Edited';
/* Create/Resume Session */
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
/* Variables */
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestURL);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationURL . '?oauth_token=' . $requestToken['oauth_token']);
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestURL);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackURL);
} else {
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceURL = "$apiURL/products/?order=entity_id&filter[0][attribute]=sku&filter[0][in][0]=" . $product_sku;
//echo $resourceURL;
//exit;
$oauthClient->fetch($resourceURL, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
$productList = json_decode($oauthClient->getLastResponse());
}
} catch (OAuthException $e) {
echo '<pre>';print_r($e);echo '</pre>';
}
/* Get price of the product SKU */
if ($productList) {
foreach ($productList as $product) {
echo '<br><br>Price of <b>' . $product_sku . '</b> is <span style="color: #ff0000; font-weight: bold;">£' . round($product->price, 2) . '</span>';
}
} else {
echo '<br><br>Product SKU <b>' . $product_sku . '</b> does not exist in the database.';
}
?>
谢谢。
答案 0 :(得分:0)
您可以使用以下代码段来避免这种情况。
$product_sku = "";
if(isset($_POST['product_sku'])){
$product_sku = $_POST['product_sku'];
}else {
echo "product doesn't exist in the database";
}
这样,只有在提交表单时才会显示错误,一旦提交表单,错误就会消失。
答案 1 :(得分:0)
你有几个选择
您可以将查询结果存储在会话中,然后重定向回表单,而不是将代码移动到表单中(因为分离逻辑和视图是个好主意)。使表单成为PHP页面,然后输出结果的会话内容。
//Whatever come back from your query even an array
$_SESSION['query_result'] = "...";
//You can redirect by putting
//this at the end of the script
//This url should be what you have in the browser when on the form.
$urlForForm = 'www.sitename.com/products/search';
header('Location: '.$urlForForm);
然后将html页面更改为php并在那里
<?php session_start();?>
<!--HTML FORM-->
<?php
//Check that value exists
if(isset($_SESSION['query_result'])){
//If it does output it
echo $_SESSION['query_result'];
//unset it so it does not keep showing up
unset($_SESSION['query_result']);
}
?>
如果你想将它们保持在一起,你可以检查是否为该索引设置了值
if(isset($_POST['product_sku'])){//Do stuff with SKU}
或者更确切地说,然后专门检查要设置的值,可以使用$_SERVER['REQUEST_METHOD']=='POST'
作为输出逻辑的全局检查。如果表单已提交,那么获得POST请求方法的唯一方法就是。
if($_SERVER['REQUEST_METHOD']=='POST'){
//All your form processing and posting code.
//Will only run when form is submitted
}