2

 <script>
  function myFunction() {
    var query = '?';
    var str = $( "form" ).serialize();
    $( "#results" ).text( query += str );
    
  }
  $( "input[type='sel'], input[type='text']" ).on( "submit", myFunction );
  $( "select" ).on( "submit", myFunction );
   myFunction();
  
</script>
echo'<input  id="submit" type="submit" value="SUBMIT" class="btn btn-success"  name="submit" onclick="myFunction()"/> ';
echo '<p id="results">&nbsp;</p></form>';
here it is javascript for to generate query string for controls so I have butten with id=submit and event onclick=myFunction

how to make this work onclick event and also query string should print on the

tag and what will be the code for insertion please let me knoe the answer

for eg: ?gender='Male'&city='dwd'

  • `type="submit"` will make the page submit and not allow the onclick event properly – pokeybit May 16 '17 at 12:12
  • then please tell in detail what I should do – Shruti Akki May 16 '17 at 12:16
  • Without cycling through all variables you cannot do this. It's strange to require single quotes around your GET variables. TIP: for every field in your form, add a onkeyup event to take the contents, remove single quotes if it contains any, then add one either side of the contents. – pokeybit May 16 '17 at 12:19
  • if you specify why you want to generate query string.. that would be great :) – Zedex7 May 16 '17 at 12:31
  • sorry I'm getting what you are going to tell – Shruti Akki May 16 '17 at 12:31
  • using that query passing to php using ajax so I have to insert those values into a table so I need query string – Shruti Akki May 16 '17 at 12:33
  • query string is generated only when your _method_ is GET. Let me clear you want to get values of html control in php file.. right.? – Zedex7 May 16 '17 at 12:38
  • ya you are correct. i.e controls are generating dynamically so i need pass values fron javascript to php using ajax – Shruti Akki May 16 '17 at 12:45
  • you need to check this.. http://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refresh#16616315 it doesn't matter if you are generating controls dyanamically – Zedex7 May 16 '17 at 12:47
  • ya I had gone your link here the problem is every time the number controls are not same it 'll be changing as need so am facing problem – Shruti Akki May 16 '17 at 12:54

1 Answers1

0

To save the query string in the database you don`t need to do this in javascript, see the following example:

test.php

<form method="post">
    <label> Name
        <input name="query[name]" type="text">
    </label>

    <label> Email
        <input name="query[email]" type="text">
    </label>

    <label> Phone
        <input name="query[phone]" type="text">
    </label>

    <button type="submit">Submit</button>
</form>

<?php

if ($_POST && isset($_POST['query'])) {
    $query = '?'. http_build_query($_POST['query']);

    //save the value of query in the database
    echo $query;
}

The above example submit the values of all inputs in an array and build the query using http_build_query

and using ajax:

test.php

<script type="text/javascript" src="/js/jquery.min.js"></script>
<form method="post">
    <label> Name
        <input name="query[name]" type="text">
    </label>

    <label> Email
        <input name="query[email]" type="text">
    </label>

    <label> Phone
        <input name="query[phone]" type="text">
    </label>
    <button type="submit" >Submit</button>
</form>
<script>
    $('form').on('submit',function(e){
        e.preventDefault();
        e.stopPropagation();

        $.ajax('/ajax.php', {
            method: 'POST',
            data: $('form').serialize()
        })
    });
</script>

ajax.php

<?php
if ($_POST && isset($_POST['query'])) {
    $query = '?'.http_build_query($_POST['query']);

    //save the value of query in the database
    echo $query;
}
rafrsr
  • 1,940
  • 3
  • 15
  • 31