Home > Software engineering >  Action form url changed after submission
Action form url changed after submission

Time:09-24

I found the action url on the form submission changed when the form was submitted, it happened after I added the id on the form to run the javascript. after I submitted the form, the url changed to index.php, I've tried looking to several sources but haven't found a solution yet.

this is my code:

<form id="kt_form" class="form" method="POST" action="src/crud/add-score/save.php">
  <div class="form-group row">
    <label class="col-auto col-form-label font-weight-bolder">
                <span>Name :</span>
            </label>
    <div class="col-6">
      <input type="text" class="form-control" name="name" />
    </div>
  </div>
  <div class="form-group row">
    <label class="col-auto col-form-label font-weight-bolder">
                <span>Address:</span>
            </label>
    <div class="col-6">
      <input type="text" class="form-control" name="address" />
    </div>
  </div>
  <button type="submit" name="save" class="btn btn-warning mr-2">save</button>
</form>


  <script>
var KTFormWidgetsValidation = function() {
  var _initValidation = function() {
    validator = FormValidation.formValidation(
      document.getElementById('kt_form'), {
        fields: {
          name: {
            validators: {
              notEmpty: {
                message: 'field is required'
              }
            }
          },
          address: {
            validators: {
              notEmpty: {
                message: 'field is required'
              }
            }
          },
        },
        plugins: {
          trigger: new FormValidation.plugins.Trigger(),
          submitButton: new FormValidation.plugins.SubmitButton(),
          defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
          bootstrap: new FormValidation.plugins.Bootstrap({
            eleInvalidClass: '',
            eleValidClass: '',
          })
        }
      }
    );
  }
  return {
    init: function() {
      _initValidation();
    }
  };
}();
jQuery(document).ready(function() {
  KTFormWidgetsValidation.init();
});
</script>

save.php

    <?php
include 'config.php';
session_start();

if (isset($_POST['save'])) {
  if ($_SERVER["REQUEST_METHOD"] == "POST") {

$nm    = $_POST['name'];
$ad    = $_POST['address'];

$save = $config->query("INSERT INTO people VALUES ('', '$nm', '$ad')");

if ($ave) {
      echo "<script>alert('saved success'); window.location.href='index.php?page=people'</script>"
}else{
echo "<script>alert('save fail'); window.location.href='index.php'</script>";
}
}else {
  header('Location: index.php');
}
?>

CodePudding user response:

In your if statement you check the $ave variable which doesn't exist. Should this be $save?

Also you have two else statements for one if statement, that won't work. Change the middle one to else if (!$save) or remove one of the else{} statements.

CodePudding user response:

You need to change the url in the action, absolute path will not work.

<form id="kt_form" class="form" method="POST" action="src/crud/add-score/save.php">

replace the above with

<form id="kt_form" class="form" method="POST" action="save.php">

or use, if save.php is in root folder

<form id="kt_form" class="form" method="POST" action="/save.php">

or use url like

<form id="kt_form" class="form" method="POST" action="http://example.com/src/crud/save.php">
  • Related