I'm working for the time being in a local environment using MAMP on mac to create a form where users can send in pricing data on food products. The results are stored in a mysql database. The data is, however, meant to compare prices between Norway and Sweden, so the validation form takes the product name and translates it into Swedish for a variable to be submitted into the database along with the rest of the users' input. I do this using cURL, and then I run the header function to redirect to a page that says the item has been registered. The cURL function works like a charm, and the data is sent into the database, but it refuses to redirect to the page that informs the user of a successful registration. So it has to be that the header("Location:") function just refuses to work. I've already set CURLOPT_FOLLOWLOCATION as well as CURLOPT_RETURNTRANSFER to true. I'm wondering if anyone has any fixes.
This is the questions on stack overflow I've already looked at:
Why the header function is not working after CURL call?
I also looked at documentation for answers, but there doesn't seem to be a lot out there. So either I'm making a dumb mistake or this is just that hyper-specific of a problem.
Here is the registration page where the user fills in input about the norwegian product.
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Matvare registreringsskjema</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register vare</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<!--index 0-->
<div class="input-group">
<label>Produkt</label>
<input type="text" name="Produkt" value="<?php echo $Produkt; ?>">
</div>
<!--index 1-->
<div class="input-group">
<label>Pris</label>
<input type="text" placeholder="xxxkr" name="Egentlig_pris" value="<?php echo $Egentlig_pris; ?>">
</div>
<!--index 2-->
<div class="input-group">
<label>Pris per kg</label>
<input type="text" placeholder="xxxkr" name="Pris_per_kg" value="<?php echo $Pris_per_kg; ?>">
</div>
<!--index 3-->
<div class="input-group">
<label>Butikkjede</label>
<input type="text" name="Butikkjede" value="<?php echo $Butikkjede; ?>">
</div>
<!--index 4-->
<!--<div class="input-group">
<label>Dato</label>
<input type="date" placeholder="DD/MM/YYYY" name="Dato" value="<?php echo $Dato; ?>">
</div>-->
<!--index 5-->
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Registrer</button>
</div>
<!--Button which guides the user to the price-log page-->
<button class="btn" onclick="location.href='http://localhost:8888/display.php'" type="button">Prishistorikk</button>
</form>
</body>
</html>
Here is the validation page it's sent to, which is the page that's included at the top of the html page. Sorry for the blending of norwegian and english in the code, but the important things to notice are $txt_for_translator is the variable that takes the input from the "Produkt" field in register.php. It's eventually put into the variable "$oversettelse" (translation).
<?php
session_start();
// Sets values
$Produkt = "";
$Egentlig_pris = "";
$Pris_per_kg = "";
$Butikkjede = "";
$Dato = date("Y-d-m");
$errors = array();
$txt_for_translator = "";
$oversettelse = "";
$apiKey = 'not tryna let people use my api now that google charges';
// Connect to database
//$db = mysqli_connect('localhost', 'root', '', 'b019-g25');
$db = new mysqli('localhost', 'root', 'root', 'b019-g25');
$db->set_charset("utf8");
//$apiKey = 'not tryna let people use my api now that google charges';
//These two lines included with same line integrated in dsiplay.php with some adjustments will show æøå within the browser page and database.
// Register products
if (isset($_POST['reg_user'])) {
// Recive all inputs from the form
$Produkt = mysqli_real_escape_string($db, $_POST['Produkt']);
$Egentlig_pris = mysqli_real_escape_string($db, $_POST['Egentlig_pris']);
$Pris_per_kg = mysqli_real_escape_string($db, $_POST['Pris_per_kg']);
$Butikkjede = mysqli_real_escape_string($db, $_POST['Butikkjede']);
//$Dato = mysqli_real_escape_string($db, $_POST['Dato']);
// This section checks if the form is filled in correctly by adding (array_push()) corresponding error unto $errors array
if (empty($Produkt)) { array_push($errors, "Må fylle inn produkt!"); }
if (empty($Egentlig_pris)) { array_push($errors, "Må fylle inn egentlig pris!"); }
if (empty($Pris_per_kg)) { array_push($errors, "Må fylle inn pris per kg!"); }
if (empty($Butikkjede)) { array_push($errors, "Må fylle inn butikkjede!"); }
//if (empty($Dato)) { array_push($errors, "Må fylle inn dato!"); }
The above is basically just getting all the input from Register.php and checking that everything is filled in. Then I check for any errors (aka just to check if any field is not filled in). Then if everything is in order, I run the cURL function and get my translation which I put into the variable "$oversettelse"
// Register product if there are no errors in the form
if (count($errors) == 0) {
$txt_for_translator = mysqli_real_escape_string($db, $_POST['Produkt']);
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($txt_for_translator) . '&source=no&target=sv';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle);
$oversettelse = $responseDecoded['data']['translations'][0]['translatedText'];
echo "<script>console.log('$oversettelse')</script>";
$query = "INSERT INTO skjema (Produkt, Oversettelse, Egentlig_pris, Pris_per_kg, Butikkjede, Dato)
VALUES('$Produkt', '$oversettelse', '$Egentlig_pris', '$Pris_per_kg', '$Butikkjede', '$Dato')";
echo "<script>console.log('Done with inserting the query')</script>";
mysqli_query($db, $query);
echo "<script>console.log('Done with mysqli_query')</script>";
$_SESSION['Produkt'] = $Produkt;
echo "<script>console.log('Done Session:produkt')</script>";
$_SESSION['success'] = "Varen er registert";
echo "<script>console.log('product is registered')</script>";
header("Location: http://localhost:8888/done.html");
//exit;
}
}
?>
And then at the end it should by redirecting to done.html which I've included below. I know that you're technically supposed to have an exit function at the very end, and I've commented it away but it just didnt't seem to make a difference. And all the echos to script log in the last section are just a way to check that the functions are actually completed (probably a noob way to do so, but it's what I'm used to). If anybody has any idea about how to fix this it would be greatly appreciated! Otherwise any tips on testing for errors would also be helpful. Thanks in advance!