Baldráni
Baldráni

Reputation: 5640

Show a different value from an input that what will be received as php

I'd like to know if there is a way to show in an input of type text a different value than the one sent to PHP.

For example, let say you have :

<input type="text" value="John"> that display, John.

Can I change the value to "Doe" for the user but keep it to "John" for PHP?

Can I achieve this using the difference between $.attr('value') and $.val()?

I ran a couple of tests and it seems that I will have to reverse it directly in my controller. Is there another solution?

Here is a little jSFiddle to play with.

Upvotes: 19

Views: 52731

Answers (4)

jesusWalks
jesusWalks

Reputation: 355

Why not use a data-value attribute for the value you want to use in the PHP?

$("form").on( "submit", function( event ) {
  event.preventDefault();
  $('input[name="name"]').val($(event).data('value'));
  $("form").submit();
});

Something like that?

Upvotes: 0

simUser
simUser

Reputation: 840

1. you shall add a variable name like

<input type="text" name='John' value="Doe">

in PHP a var_dump('John') will result in 'Doe'

but obviously this is not very clever way, especially in case you have more then one input field, so I suggest

2. using an array like

<input type="text" name='person[John]' value="Doe">
<input type="text" name='person[Fred]' value="Flintstone">

in PHP you can selectively test like

if( isset($_POST['person']['John']) ) {
   ... do something ...
}elseif( isset($_POST['person']['Fred']) ) {
   ... do something different...
}

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

If you're open to a (pure) PHP solution, sure you can.

Sidenote: It's best to have a serverside method (as a Plan-B), should the end-user disable Javascript.

Given the user will replace the "already shown" value in the input though.

The input needs to hold a name attribute in order for this method to work.

<?php 

if(!empty($_POST['name'])){

   echo $name = $_POST['name']; // Will echo what was replaced in the input

}

else{
 echo $name = $_POST['name']; // Will echo the value that was already set in the input
}

?>


<form method="post">

<input type="text" value="John" name="name">

<input type="submit">

</form>

Note: echo $var = "something"; is valid syntax.


Edit:

It's unclear as to what you want to achieve here. However you can use a hidden attribute and remove value="John" from the input.

The user will need to fill the input.

The following will only output "Doe" if the input was left blank, yet output both if it is filled.

<?php 

if(!empty($_POST['name'])){

   echo $name = $_POST['name'];

}

    else{
     echo $name = $_POST['name'];
    }


if(isset($_POST['hidden_name_value'])){

   echo $hidden = $_POST['hidden_name_value'];

}



?>


<form method="post">

<input type="text" name="name">

<input type="hidden" name="hidden_name_value" value="Doe">

<input type="submit">

</form>

Upvotes: 0

Machavity
Machavity

Reputation: 31644

An odd request to be sure but...

You can't change the field's value and just do a simple form submission. The field will dutifully send whatever is in it. There's a few hackery ways around this tho

Option 1 - Hidden fields

So make a field, disable it, and add a hidden field. Disabled fields are never successful, although the user will be unable to change the field value and many browsers will change the styling of the field automatically

<input type="text" name="name" value="John" disabled>
<input type="hidden" name="name" value="Doe">

Option 2 - Change the value on submit

As you mentioned, you can always change the value when the form is submitted. The below listener will capture the form submitt Using jQuery since you asked it that way

$("form").on( "submit", function( event ) {
  event.preventDefault();
  $('input[name="name"]').val('Doe');
  $("form").submit();
});

Upvotes: 20

Related Questions