I am trying to build a feedback option with php and MySql and I was wandering how I could stock the current URL on the database ?
Here's my post method (works fine for the feedback):
<form method="post" action="feedback.php" style="margin-top:-90px;">
<p>
<input type="radio" name="radios" id="poor" value="poor" /><label for="poor">Poor</label>
<input type="radio" name="radios" id="average" value="average" checked="checked" /><label for="average">Average</label>
<input type="radio" name="radios" id="good" value="good" /><label for="good">Good</label>
<input type="submit" value="Send" />
</p>
</form>
And this is my function that put the information in the database:
<?php
try {
$bdd = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', '');
}
catch(Exception $e) {
die('Erreur : ' .$e->getMessage());
}
if ($_POST['radios'] == 'good') {
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES (\'\', \'\', \'Good\')');
} else if ($_POST['radios'] == 'average') {
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES (\'\', \'\', \'Average\')');
} else {
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES (\'\', \'\', \'Poor\')');
}
header('Location: Lexcelera_Translator.php');
?>
So how can I get the URL from the page where the form is and add it to my data base?
So just to avoid too many comment on the same post and make it easier to understand:
I now have <input style="display:none;" name="url" value="<? echo 'http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]';?> "/> in my post form on my index.php
and I'm using
if ($_POST['radios'] == 'good') {
$url = $_POST['url'];
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. mysql_real_escape_string($url) .'", "Good")');
}
to store it in my database like wlard suggested. But instead of the actual url, I got <? echo 'http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]';?>
stored in MySql instead. Any idea? :(
put an invisible form field in your form like
<input style="display:none; name="url" value="<? echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>">
Use the below code in your form tag
<input type="hidden" name="url" value="<?php echo 'http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];?> "/>
In your you function(db insertion function), you can take the value as $_POST['url']
Ok so thank you very much for your help! Here I post the final working version:
<form method="post" action="feedback.php" style="margin-top:-90px;">
<p>
<input type="radio" name="radios" id="poor" value="poor" /><label for="poor">Poor</label>
<input type="radio" name="radios" id="average" value="average" checked="checked" /><label for="average">Average</label>
<input type="radio" name="radios" id="good" value="good" /><label for="good">Good</label>
<input type="hidden" name="url" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?> "/>
<input type="submit" value="Send" />
</p>
</form>
And the feedback.php:
<?php
try {
$bdd = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', '');
}
catch(Exception $e) {
die('Erreur : ' .$e->getMessage());
}
if ($_POST['radios'] == 'good') {
$url = $_POST['url'];
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Good")');
} else if ($_POST['radios'] == 'average') {
$url = $_POST['url'];
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Average")');
} else {
$url = $_POST['url'];
$bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Poor")');
}
header('Location: index.php');
?>
Hope this can be useful to someone else :)