Jump to content

translate my code please; javascript to php


shahriar
 Share

Recommended Posts

translate my code please; javascript to php. very very thanks.

 

<!DOCTYPE html>
<html>
<body>
<form>
<input type="checkbox" name="myfilter" value=",photo">With tag1<br>
<input type="checkbox" name="myfilter" value=",pic">With tag2<br>
<input type="checkbox" name="myfilter" value=",cream">With tag3<br>
<input id="year1" value="">
<input id="month1" value="">
<input id="day1" value="">
<br>
<input id="search1" value="">
<br>
</form>
<button onclick="myFunction()">Search It!</button>
<p id="demo"></p>


<script>
function myFunction() {
  var myfilter = document.forms[0];
  var txt = "";
  var i;
  for (i = 0; i < myfilter.length; i++) {
    if (myfilter[i].checked) {
      txt = txt + myfilter[i].value;
    }
      var year = "&year=" + document.getElementById("year1").value;
          var month = "&month=" + document.getElementById("month1").value;
             var day = "&day=" + document.getElementById("day1").value;
                var search = "&s=" + document.getElementById("search1").value;
  }
  var zzz = "http://test.com/class/category-photography/?tag=" + txt + year + month + day + search;
 window.location.assign(zzz)
}
</script>
</body>
</html>

 

Link to comment
Share on other sites

While it's usually not possible to translate Javascript to PHP, in this particular case the same result can be achieved using PHP. You an replace the onclick with a form submission and window.location can be replaced with an HTTP location header.

Link to comment
Share on other sites

You should go through the effort of learning PHP rather than asking people to write code for you. Most of us have jobs and can't spend the time writing code if we're not getting paid for it, but just to illustrate my previous post, I'll write the code for this example.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  $data = [
    'tag'    => implode(',', $_POST['txt']),
    'year'   => $_POST['year'],
    'month'  => $_POST['month'],
    'day'    => $_POST['day'],
    's' => $_POST['s']
  ];
  $target = 'http://test.com/class/category-photography/?' . http_build_query($data);
  header("Location: {$target}");
  exit;
}
?>
<!DOCTYPE html>
<html>
<body>
  <form method="POST">
    <input type="checkbox" name="txt[]" value="photo">With tag1<br>
    <input type="checkbox" name="txt[]" value="pic">With tag2<br>
    <input type="checkbox" name="txt[]" value="cream">With tag3<br>
    <input name="year" value="">
    <input name="month" value="">
    <input name="day" value="">
    <br>
    <input name="s" value="">
    <br>
    <button type="submit">Search It!</button>
  </form>
</body>
</html>

 

  • Like 1
Link to comment
Share on other sites

7 hours ago, Ingolme said:

You should go through the effort of learning PHP rather than asking people to write code for you. Most of us have jobs and can't spend the time writing code if we're not getting paid for it, but just to illustrate my previous post, I'll write the code for this example.


<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  $data = [
    'tag'    => implode(',', $_POST['txt']),
    'year'   => $_POST['year'],
    'month'  => $_POST['month'],
    'day'    => $_POST['day'],
    's' => $_POST['s']
  ];
  $target = 'http://test.com/class/category-photography/?' . http_build_query($data);
  header("Location: {$target}");
  exit;
}
?>
<!DOCTYPE html>
<html>
<body>
  <form method="POST">
    <input type="checkbox" name="txt[]" value="photo">With tag1<br>
    <input type="checkbox" name="txt[]" value="pic">With tag2<br>
    <input type="checkbox" name="txt[]" value="cream">With tag3<br>
    <input name="year" value="">
    <input name="month" value="">
    <input name="day" value="">
    <br>
    <input name="s" value="">
    <br>
    <button type="submit">Search It!</button>
  </form>
</body>
</html>

 

very thanks buddy. very thanks.

I know you have job. I only asked for help if anyone was interested and could help.

You have been very kind to me :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...