Home » Php » javascript – Change input text value in before submitting form

javascript – Change input text value in before submitting form

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to change the value of my input text field before submitting the form using jQuery like this:

<form actions="http://test.com/" method="GET">
 <input name="test" id="autocompleteform" type="text"/>
</form>
<script>
$('#form-customer-attr-new').submit(function(e) {
    var value = $("#autocompleteform").val();
    value = value.substr(0, value.indexOf(' '));
    if (!isNan(value) && !empty(value)) {
        $("#autocompleteform").val(value);
        alert($("#autocompleteform").val());
        return true;
    } else {
        alert("Invalid Postcode");
        return false;
    }
});
</script>

when i alert the value of the input file, it’s showing the new value, but when the form submitten, the paramether in url still showing the old value of the input, for example:

 old_input_value = "1234 justice spoiler message";
 new_input_value = "1234";
 the_url_after_form_submit_now = "http://test.com?test=1234+justice+spoiler+message";
 the_url_after_form_submit_should_be ="http://test.com?test=1234";
How to&Answers:
<form action="" id="form_id">
    <input type="text" name="change_value" id="change_value">
    <input type="text" name="d" id="d">
    <input type="submit" name="">

</form>    

$("#form_id").on("submit", function (e) {
        e.preventDefault();//stop submit event
        var self = $(this);//this form
        $("#change_value").val("deneme");//change input
        $("#form_id").off("submit");//need form submit event off.
        self.submit();//submit form
    });

Answer:

Couple of things:

  • id of form was not set, add id=”form-customer-attr-new” to form tag
  • isNan should be isNaN
  • !empty should be !!

Now it should work. full working example:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, 
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <script
            src="https://code.jquery.com/jquery-3.2.1.min.js"
            integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
            crossorigin="anonymous"></script>
</head>
<body>
<form actions="http://test.com/" method="GET" id="form-customer-attr-new">
<input name="test" id="autocompleteform" type="text"/>
<input type="submit" />
</form>

<script>
    $('#form-customer-attr-new').submit(function(e) {
    var value = $("#autocompleteform").val();
    value = value.substr(0, value.indexOf(' '));


    if (!isNaN(value) && !!(value)) {
        $("#autocompleteform").val(value);
        alert($("#autocompleteform").val());
        return true;
    } else {
        alert("Invalid Postcode");
        return false;
    }
});

</script>
</body>

Answer:

Here is a slightly different approach, but I believe that it should work.
(1) Create a named function for your logic (I corrected/changed some syntax that was problematic)

<script type="text/javascript" language="javascript">
function prepSubmittal()
{
    var result = false;
    var value = null;

    try
    {
        value = $("#autocompleteform").val();

        if(value.indexOf(" ")>-1) value = value.substring(0, value.indexOf(" "));

        if (!isNaN(value) && value.trim().length>0) 
        {
        $("#autocompleteform").val(value);
        result = true;
        } 
            else 
            {
        alert("Invalid Postcode");
        result = false;
        }
    }
    catch(e)
    {
        result = false;
        alert("prepSubmittal Error:  " + e.Message);
    }
    finally
    {

    }

    return result;
}


</script> 

(2) Change your form element to the following (note you had actions attribute instead of action and I added onsubmit=”return prepSubmittal()”)

<form action="http://test.com" method="GET" onsubmit="return prepSubmittal()">

Let me know how it goes.

Answer:

You submit the form before you change the value. You have to submit the form using jQuery, after you change the value.

Change the submit button to link to jquery function, change all you have to change and then submit form using:

$("#formId").submit();

Answer:

To accomplish that you need to:

  1. Prevent form submission
  2. Change input value
  3. submit form with jquery $('#form').submit()

Prevent form submission with e.preventDefault() right inside your callback handler

Answer:

Use your custom submit via ajax. i.e.,

<form id="form-customer-attr-new" actions="http://test.com/" method="GET">
 <input name="test" id="autocompleteform" type="text"/>
</form>
<script>
    $('#form-customer-attr-new').submit(function(e) {
        var value = $("#autocompleteform").val();
        value = value.substr(0, value.indexOf(' '));
        var urlPost = 'http://test.com/'
        if (!isNan(value) && !empty(value)) {
            $("#autocompleteform").val(value);
            alert($("#autocompleteform").val());
            $.ajax({
                   type: "GET",
                   url: urlPost+'?test='+value  ,
                   data: '',
                   dataType: "text",
                   success: function(resultData){
                            alert("Save Complete");
                   }
        } else {
            alert("Invalid Postcode");
            //return false;
        }
    });
</script>

Answer:

Submit API for jQuery

The handler will execute as a response to the $('#form-customer-attr-new').submit() (it’s a callback function practically), so you will need to move the code from that function to before you do the actual submit.

So move the code right before the jQuery submit() call.

Also, i see that your id is not added to the HTML, please do that as the call will not work otherwise.

Answer:

Step 1: call a java script function while form submitted

<form onsubmit="return test();">

Step 2: inside test function set value in your text field.

<script>
function test()
{
   document.getElementById("myTextFieldId").value = "12345";
   return true;
}
</script>

Answer:

<form id="form-customer-attr-new" action="" method="get">
 <input name="test" id="autocompleteform" type="text" value=""/>
</form>

<script>
$('#form-customer-attr-new').submit(function(e) {
    var value = $("#autocompleteform").val();
    value = value.substr(0, value.indexOf(' '));

    if (!isNaN(value) && value!='') {
        $("#autocompleteform").val(value);
        alert($("#autocompleteform").val());
        return true;
    } else {
        alert("Invalid Postcode");
        return false;
    }
});
</script>