1

I'd like to change the url of the button when they hit submit if they are on a mobile screen 480 width or less.

Example:

<form name="myform1" action="mylink.php" method="post"> 
<input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

But on mobile devices 480 wide or less I'd like it to change to:

<form name="myform1" action="mylink-mobile.php" method="post"> 
<input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

The page is PHP but I'm thinking I can use javascript screen.width to detect <= 480 but I'm not sure how to dynamically change the form action url based upon the screen width. How would I do this?

KeithW
  • 31
  • 6

5 Answers5

1

Well without JavaScript you can use CSS media to show and hide the different forms.

form[name="myform1"] {
    display: block;  
}

form[name="myform2"] {
    display: none;  
}


@media (max-width: 400px) {

  form[name="myform1"] {
    display: none;  
  }

  form[name="myform2"] {
    display: block;  
  }
  
}
<form name="myform1" action="mylink.php" method="post"> 
<input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

<form name="myform2" action="mylink-mobile.php" method="post"> 
<input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

If you want to use JavaScript to change the action, you want to read the window size

Community
  • 1
  • 1
epascarello
  • 185,306
  • 18
  • 175
  • 214
1

You can do this with no JavaScript at all.

<form name="myform1" class="wide sense" action="mylink.php" method="post"> 
  <input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form>
<form name="myform1" class="narrow sense" action="mylink-mobile.php" method="post"> 
  <input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

Include both forms with different class values. Then in your CSS:

form.sense { display: none; }
@media screen and (min-width: 481px) {
  form.sense.wide { display: block; }
}
@media screen and (max-width: 480px) {
  form.sense.narrow { display: block; }
}

That'll work when a user goes from portrait to landscape without you having to monitor the orientation or screen geometry from JavaScript.

Pointy
  • 371,531
  • 55
  • 528
  • 584
0

HTML

<form name="myform1" action="mylink.php" method="post"> 
<input type="image" name="submit" src="/Button1.jpg" border="0" alt="Submit" />
</form> 

JS

window.onload = function (){
if (window.innerWidth <= "480")
document.getElementsByTagName("form")[0].setAttribute("action", "mylink-mobile.php");
}
Kawinesh SK
  • 2,998
  • 1
  • 13
  • 29
  • Would the tag name be myform1 vs form in your example since there may be multiple forms on a page? – KeithW Mar 30 '15 at 13:57
0

The idea is to detect if they're on a mobile device and act accordingly, right? After all, you're changing the form submission action, so I'm assuming something different is going on behind the scenes.

That said, I would go with mobile device detection instead of using the screen size. See this thread for more info (disregard the "jquery" part as the information provided has non-jquery solutions as well): What is the best way to detect a mobile device in jQuery?

The majority seem to use user agent detection to determine if you're on mobile using something like this:

if (navigator.userAgent.match(/Mobi/)) {
    // mobile!
}
Community
  • 1
  • 1
wholevinski
  • 2,978
  • 13
  • 19
0

You could define two submit button with a different value attribute

<form action="action.php" method="post"> 
   ...
   <input type="submit" name="submit" value="480" />
   <input type="submit" name="submit" value="over480" />
</form> 

and you show just one input, depending on the viewport size

CSS

[value="480"] { display: none; }
[value="over480"] { display: block; }

@media all and (max-width: 480px) {
    [value="480"] { display: block; }
    [value="over480"] { display: none; }
}

then in the action.php check which one actually submitted your form and — depending on that parameter sent via POST — apply a different server side logic.

Pros:

  • no need to duplicate the markup of the form
  • no need to use javascript to detect a size change of the viewport
  • the server side logic is in one single entry point

Example (github gist):
https://gist.github.com/anonymous/64b9e1d42c00356f5874

Fabrizio Calderan loves trees
  • 109,094
  • 24
  • 154
  • 160