Jump to content

Store Values From Add & Remove Select Lists


shahzad429

Recommended Posts

I have use this This to make add/remove select list

 

 

 

<form action="home.php?action=test" method="post" id="myform" name="myform" onsubmit="ValidatePageForm()">

<fieldset>

<select name="selectfrom" id="select-from" multiple size="5">

<option value="1">Item 1</option>

<option value="2">Item 2</option>

<option value="3">Item 3</option>

<option value="4">Item 4</option>

</select>

<a href="Javascript:void(0);" id="btn-add">Add &raquo;</a>

<a href="Javascript:void(0);" id="btn-remove">&laquo; Remove</a>

<select name="selectto" id="select-to" multiple size="5">

<option value="5">Item 5</option>

<option value="6">Item 6</option>

<option value="7">Item 7</option>

</select>

</fieldset>

<input name="submit" type="submit" value="submit" />

</form>

 

using below script i can see values of selected options

 

 

<script type="text/javascript">

function ValidatePageForm()

{

$('#select-to option').each(function(){

alert($(this).val());

});

}

</script>

 

 

But how can i take these values and store them in database?? because when i submit i dont get see data in post dump.

any help regarding this please.

Thanks, Shahzad

Link to comment
Share on other sites

So, your question is about storing them in the db? Or is it getting the data to a PHP page to be dealt with?

 

If it's the first, are you trying to do it without having to refresh the page, or give a custom action after posting the form? Otherwise this would be purely a PHP question, but I'll help anyways just please clarify it.

 

If you are just posting it, then the HTML form would take care of it for you. If you want JS to handle the form, $.serialize() then send the data. If you are having trouble getting the data because you're selecting parts to send from the form you have, then you could make hidden fields to have sent and deal with them like any other form item once sent to PHP.

Link to comment
Share on other sites

the goal is what ever user select to store those options in DB for later retrieval.

 

 

so what ever method is good tell me.

 

right now in front end user can add and remove option but what i want when he submit i should know when he select so that i can save them.

 

 

Thanks,

shahzad

Link to comment
Share on other sites

Well, since you need JS to use the form in the first place, we can just use JS to get it set up because JS-disabled browsers can't use the form to begin with. This isn't good practice, but you can Google about all of that if you care.

 

Create a new form. This will be what we actually submit.

var form = $(document.createElement("form"));

 

Now when you select an item, add it to the form.

var item = $(document.createElement("input")).attr({ 'name': myName, 'type': "hidden", 'value': myValue });

form.append(item);

 

When you're ready to submit, serialize the form and post it.

var postdata = form.serialize();

$.post(myURL, postdata, function(data) { /* success function */ });

 

Now you just deal with it in PHP like you would any other form.

$_POST['myName'] is equal to myValue from our JS.

 

Note that none of this is tested, so if there's a mistype or I got something wrong, sorry. (:

Edited by Xaotique
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.