0

I am using input to submit data to MySQL using PHP. It works fine in HTML input but it doesn't work using the Onsen UI framework tag ons-input. Any idea why?

Normal HTML (it works)

<form action="insert.php" method="POST">
Firstname: <input type="text" name="fname" required /><br>
Lastname: <input type="text" name="lname" require /><br>
<input type="submit" />
</form>

Onsen UI framework HTML (it does not work)

<form action="insert.php" method="POST">
Firstname: <ons-input type="text" name="fname" required />
Lastname: <ons-input type="text" name="lname" required />
<input type="submit" />
</form>

insert.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql  = "INSERT INTO exampledb (fname, lname)
  VALUES ('".$_POST["fname"]."','".$_POST["lname"]."')";

if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
Antonio Andrés
  • 169
  • 2
  • 15
  • Open the developer console, do the values submit to the PHP script? You also are open to SQL injections. – user3783243 Sep 05 '18 at 20:57
  • Well I think I'm reaching but I don't see the ONSEN scripts in the HTML. – Forbs Sep 05 '18 at 20:58
  • **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of manually building your queries like that. Specially since you're not escaping the user inputs at all! – M. Eriksson Sep 05 '18 at 20:59
  • @user3783243 What I can see is just "onsenui.min.js:2 Uncaught (in promise) TypeError: Cannot read property 'textContent' of null" – Antonio Andrés Sep 05 '18 at 21:05
  • @Forbs it is the tag – Antonio Andrés Sep 05 '18 at 21:05
  • @MagnusEriksson I am learning, so I think at this moment I can go with it. Thanks for the warning! – Antonio Andrés Sep 05 '18 at 21:06
  • @AntonioAndrés That error needs to be in the question, please add it. This is a JS issue, or an issue client side. This isn't a PHP issue. – user3783243 Sep 05 '18 at 21:11
  • If you're learning, you should aim to do things properly from the start. Bad habits are hard to shake and there's a big risk that you will forget/miss to fix things like that later. That's how sites get hacked. – M. Eriksson Sep 05 '18 at 21:38
  • @MagnusEriksson Spotted on, thanks for the advice! – Antonio Andrés Sep 05 '18 at 21:43
  • @MagnusEriksson Anyway, any clue why the above HTML code does not work? Can't find a reason. – Antonio Andrés Sep 06 '18 at 08:00

0 Answers0