2

I have this line of code

        <html>
        <head></head>
        <body>
        <script>
$(document).ready(function(){
$("#update").click(function(){
$("#id2").css("display","hidden");
var r = $("#id2").val()+1;
$("#id2").val(r);
});
});
</script>
        <title>CV Education Form</title>
       .
       .
       .
       .
        </fieldset>
        <input type="text" name="id" id="id2" value="<?php echo ($id == 0 ? 1 : $id );?>"/>
        <input type="submit" value="Update" name="submit"/>
        </form>
        </body>
        </html>

and I want to change it to hidden so it is invisble but also when i click on a button update (which I have it) then it increments the value ($id) ... More simple i want something like that id+1.

Do you know how can I do that?

enter image description here

When I click on update button i want the 1 which is the $id to become $id+1 but I dont want to add myself I want to do it automatically when i click the update button and also hide the textfield

5
  • 1
    use js for this job. Commented Jan 13, 2015 at 8:42
  • yes but i dont know how to do it Commented Jan 13, 2015 at 8:42
  • 1
    Have you tried anything or even searched for this I am sure you can find this easily. Commented Jan 13, 2015 at 8:43
  • Maybe i am searching on the wrong way . For example i am searching this "how can I auto increment the value of an input type="hidden"". Maybe it is wrong , can you suggest me another way to search it Commented Jan 13, 2015 at 8:45
  • yupp u are searing in the wrong way search for changing input type separately and incrementing separately and u will definetly get your answer. Commented Jan 13, 2015 at 8:51

8 Answers 8

1

Just specify the update button id and id of the inputs. Use the code below

     <html>
        <head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
        <body>
        <script>
$(document).ready(function(){
$("#update").click(function(){
$("#id2").css("display","none");
var r = parseInt($("#id2").val(),10)+1;
$("#id2").val(r);
});
});

</script>
        <title>CV Education Form</title>
       .
       .
       .
       .
        </fieldset>
        <input type="text" name="id" id="id2" value="<?php echo ($id == 0 ? 1 : $id );?>"/>
        <input type="submit" value="Update" id="update" name="submit"/>
        </form>
        </body>
        </html>

Check it here http://jsfiddle.net/53cov3uq/3/

Hope this helps you

Sign up to request clarification or add additional context in comments.

8 Comments

the script tag should i place them in the php tags and also the type I want to be hidden and be incremented when i click on update button
The code above is doing all the task that you mentioned.
where should i place the script tags in the php tags or in the html tags ?
Place them in html tags
Hey listen replace #update in $("#update") with your update button od
|
1

you can do it very simply using either Jquery or JavaScript .find the code below

function update()
{
  var count=parseInt($('#counter').val());
  $('#counter').val(count+1);
  alert($('#counter').val());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input type="hidden" id="counter" name="id" value="0"/>
<button onclick="update()">update</button>

i just kept alert at the end that will popup the current value of the id.

Comments

1

try this,

<script type="text/javascript">
function increment_val(){
   var id_val = parseInt(document.getElementById('id').value);
   id_val++;
   document.getElementById('id').value=id_val;
}
</script>

and in button html call js function,

<input type="button" value="update" id="btn_id" onclick="increment_val();"/>

2 Comments

I edited my question can you please be more specific where should i place it
i assumed you didn't used jQuery. Place the Code inside <script> tag in head or after body. Use this button for update
0

For that use javascript. Php is server-side script.

<script type="text/javascript">
  function increment()
  {
    var elem = document.getElementById("hiddenElement");
    elem.value = 1 + parseInt(elem.value);
  }
</script>
<input type="hidden" id="hiddenElement" name="id" value="0" />
<button onclick="increment()">Click me</button> 

Comments

0

You can use below code which increments the value on click of a button.

<button onclick="document.getElementById('id').value = document.getElementById('id').value + 1;">Update</button>

Comments

0

HTML

<input type="text" name="id" value="<?php echo ($id == 0 ? 1 : $id );?>" />

To change input type you can use the syntax like:

$("#id").attr('type','hidden');

To increment the value use

document.getElementById('id').value=parseInt(document.getElementById('id').value)+1;

or using jquery

$("#id").val($("#id").val()+1);

Hope it helps.

Comments

0

try this if you want to change text to hidden element and increment value

HTML Code:

<input type="text" name="id" value="<?php echo ($id == 0 ? 1 : $id );?>" />
<button onclick="increment_value()">Update</button>

JS:

function increment_value()
{
    var txt_field = document.getElementsByName("id")[0];
    txt_field.value = parseInt(txt_field.value) + 1;
    txt_field.setAttribute("type", "hidden");
}

OR

If you want to simply hide the text field just use this version of the above function

function increment_value()
{
    var txt_field = document.getElementsByName("id")[0];
    txt_field.value = parseInt(txt_field.value) + 1;
    txt_field.style.display = "none";
}

Comments

-1
<input type="hidden" name="id" id="id" value="<?php echo ($id == 0 ? 1 : $id );?>" />

Jquery

$(function(){
  $("button").click(function(){
    $("#id").val($("#id").val()+1);
  })
})

2 Comments

OP wants to change input type to hidden on an event.
I will update my question with a print screen check in a min.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.