Mr Johnson
Mr Johnson

Reputation: 65

Add Value To URL On Form Submit With Jquery

I have this form below when i submit it i want it to append a value to the 6 urls generated from a php query.

<form id="emailform" action="" method="post">
      <input type="text" value="" size="30" name="email" id="email">
      <input type="submit" name="submit" value="Submit" class="continue-button">
</form>

This is an example of my link, sitename.com/demo.php?1=demo&2=haha How can i use the form and jquery to append &email=formvalue to the url so sitename.com/demo.php?1=demo&2=haha = sitename.com/demo.php?1=demo&2=haha&email=formvalue ?

Please keep in mind that these urls are generated dynamically with a query.

I tried using attr on a but it doesnt work. I know you have to check for the submit then use attr.

$('#emailForm').submit(function(){ //listen for submit event

}

Upvotes: 1

Views: 4796

Answers (3)

Dimag Kharab
Dimag Kharab

Reputation: 4519

use serilaize method

$('#emailForm').submit(function(){ 

 //listen for submit event
 var all_submit_data = $( '#emailForm' ).serialize() ; 
 //this will have all input fields values in serilized 

}

ref : https://api.jquery.com/serialize/

Upvotes: 1

Tyler.z.yang
Tyler.z.yang

Reputation: 2450

You'd better to use markup input type="hidden" to submit the value Your Form :

<form action = "sitename.com/demo.php" method="post" >
    <input type="hidden" name="1" value="demo" />
    <input type="hidden" name="2" value="haha" />
    <input type="text" value="" size="30" name="email" />
    <input type="submit" name="submit" value="Submit" class="containue-button" />
</form>

Then you just need to submit this form in javascript or jquery. : )

Upvotes: 1

Johnny
Johnny

Reputation: 444

You can use hidden field like this

<form action="" method="post">
      <input type="hidden" name="new_value" value="value"/>
      <input type="text" value="" size="30" name="email" id="email">
      <input type="submit" name="submit" value="Submit" class="continue-button">
</form>

or you can add to action of form like this

<form action="http://action_url?new_value=value" method="post">
      <input type="text" value="" size="30" name="email" id="email">
      <input type="submit" name="submit" value="Submit" class="continue-button">
</form>

Upvotes: 4

Related Questions