outputs it in the structured data format required by Google for reviews
Mark E.
PHP code that creates a form to capture review data and outputs it in the structured data format required by Google for reviews:
<?php
// Set up variables to store form data
$name = $rating = $review = '';
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input data
$organname = htmlspecialchars($_POST['organname']);
$subjectname = htmlspecialchars($_POST['subjectname']);
$name = htmlspecialchars($_POST['name']);
$reviewpost = htmlspecialchars($_POST['reviewpost']);
// Output structured data for review
$review_data = array(
'@context' => 'http://schema.org/',
'@type' => 'Review',
'itemReviewed' => array(
'@type' => 'Organization',
'name'=> $organname,
),
'reviewRating' => array(
'@type'=> 'Rating',
'ratingValue' => '5',
'bestRating' => '5'
),
'name'=> $subjectname,
'author'=> array(
'@type'=> 'Person',
'name'=> $name,
),
'reviewBody'=> $reviewpost,
);
$json_ld = json_encode($review_data);
$newreview = '<script type="application/ld+json">'.$json_ld.'</script>';
// Save form data to file
$file = fopen('reviews.txt', 'a');
fwrite($file, "$newreview\n\n");
fclose($file);
// Clear form data
$name = $rating = $review = '';
}
?>
<!-- HTML form for review data -->
<form method="post">
<label for="organname">Organization Name:</label>
<input type="text" name="organname" value="<?php echo $organname; ?>"><br>
<label for="subjectname">Subject Name:</label>
<input type="text" name="subjectname" value="<?php echo $subjectname; ?>"><br>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo $name; ?>"><br>
<label for="reviewpost">Review Post:</label>
<textarea name="reviewpost"><?php echo $reviewpost; ?></textarea><br>
<input type="submit" value="Submit">
</form>
This code creates a form with three input fields for the name, rating, and review text. When the form is submitted, the input data is sanitized and used to create a JSON-LD object with the structured data for the review. The JSON-LD object is then output in the script tag, which Google can read and use to display the review in the search results.