Jump to content
shahriar

translate my code please; javascript to php

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>

 

Share this post


Link to post
Share on other sites

You can't! php produces the html, while javascript manipulates that html code depending on events after that html code is produced.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

OK! Do the same with onclick that runs the function in php, or window.location in php, and we'll see how far you get, go on. It's simple so you should be able to do those two simple things.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...