-1

I think i have searched already about tags and yes i am using them already.

But here my question or Idea is:

I have a form including input to search/insert data into database (mysql) (index.php) form action is being performed on search.php search box searches input data in the selected tables, if it is found, then it gives the records in result.php, otherwise it inserts the data in table(sql) and then shows it in result.php

I am validating the form/input using javascript on the same page even using tags Now after loading the index.php page, if someone disable javascript, it shows an alert to reload the page. if someone reload the page, searchbox will hide.

but the problem is, if after loading the page, if someone disable javascript and does not reload the page, and search some restricted text using javascript regex, that text goes into mysql database and it is saved. search.php perform its action and redirect it to result.php

I want some trick in search.php that checks some condition for javascript enabled/disabled, then perform the remaining actions.

IS THERE ANY WAY, IF ACTION SHOULD BE ANOTHER PAGE, on that page is javascript is disabled then it shows nothing, and if it is enabled then redirects to search.php

Atif Sheikh
  • 65
  • 2
  • 9
  • set a hidden input value using js on submission. if it's not set you'll know js wasn't running. – inarilo Jun 05 '17 at 19:07
  • @inarilo I am sorry, i could not get it, Can you plz give a complete code for hidden input – Atif Sheikh Jun 05 '17 at 19:16
  • i meant ``. set the value of this when submit is clicked. – inarilo Jun 05 '17 at 19:28
  • @inarilo I used this trick but this variable is passing even javascript is enabled `` – Atif Sheikh Jun 06 '17 at 11:06
  • used this `` and on the action page, `if ($nojava == "nojs" ) { header("location:index.php"); } else {` but its redirecting to index.php back even javascript is not disabled – Atif Sheikh Jun 06 '17 at 11:23

2 Answers2

0

Use a hidden input field and set its value using javascript on form submission. Since the value will be set only using javascript, if the submitted form has an empty value for this field, it can be concluded that javascript was disabled.

Using jQuery:

In the html:

<form id="myformid" action="myactionurl.php" method="POST">
   <!-- form fields here -->
   <input type="hidden" id="time" name="time" value="">
</form>

In js:

$('#myformid').submit(function(ev) {
    $('#time').val($.now());
});

In php:

if(!empty($_POST['time'])) {
    //code
} else {
    echo "Javascript must be enabled for this site to work.";
}

You don't have to use time, it can be anything.

inarilo
  • 804
  • 1
  • 9
  • 14
0

Form validation on JS level is unimportant, what you need to think about is your php validation. Please read top answers of: form validation with javascript vs php

and this: How can I prevent SQL injection in PHP?

If you need to check if JS is enabled you can simply make button for your form (but not type="submit" but just a button element). Add event to that button:

onclick="document.getElementById('FormId').submit();"

and your form will be submitted only if JS is enabled as button will use JS for submission

NoOorZ24
  • 2,914
  • 1
  • 14
  • 33
  • I m sorry but this is just a comparison for validation of any form between javascript and php, and recommendations are for javascript. it is not giving any solution what i need. btw thanks for sharing such a helpful post – Atif Sheikh Jun 06 '17 at 11:52
  • @AtifSheikh look at additional info I edited in. I'm not sure if that's what you are looking for but hope it helps – NoOorZ24 Jun 06 '17 at 12:03
  • this will fail if there is for instance one text field, and the user presses enter. – inarilo Jun 06 '17 at 14:56