webstudent
webstudent

Reputation: 41

Passing the value from JavaScript confirmation to Codeigniter Controller

I am new to JavaScript and Codeigniter. I want to know how to pass the JavaScript confirmation (Yes/No) to Codeigniter Controller.

For Example I have a link

Update all records. (how to link this?).

Once the user click it, there's a pop-up message that says Are you sure?. If yes then it will be passed to controller/model and do the updates if no it will stay on the same page.

Or can somebody teach me other approach?

Upvotes: 0

Views: 1178

Answers (4)

webstudent
webstudent

Reputation: 41

Thank you very much for all your effort for answering my question, I got an idea from you. Here is my solution.

my link is

id="my_link" href="controller/function" onclick="return confirm('Are you sure?');"> Click here

it works fine it goes to the controller/function and do the updates

thanks everyone

Upvotes: 0

Hieu Le
Hieu Le

Reputation: 8415

This is the most simple solution I can find, you can see a live demo at http://jsbin.com/ecimiw/1/edit

I use jQuery to simplize things :)

In general. I use preventDefault() to prevent the browser change the page immediately. After that, display a confirm box. If user click yes (agreed) then make the broser change to the url in the href of the clicked a element.

The most important things here is jQuery and a class name for all link you need confirmation (in this demo, I uses need-confirm).

Upvotes: 1

user1814734
user1814734

Reputation:

what do you use? radio button? if yes, just add form tags above the radio button tags.

Here is the example

<form name = "yesno" action = "<?php site_url('check/yesorno') ?>" method = "post">
    <input type = "radio" name = "yesorno" value = "yes" onclick = "this.form.submit()">Yes
    <br>
    <input type = "radio" name = "yesorno" value = "no" onclick = "this.form.submit">No
</form>

/*Note:
Form name = name of form
action = destionation url (site_url(yourController/yourFunction))
method = post/get
onclick = javascript event (will execute the code when user click)
this.form.submit() = javascript dom, means submit the your form when specified event executed*/

code if u want to use ajax

//first->create the object
//for chrome, firefox, etc
if(window.XMLHttpRequest)
{
   xmlhttp = new XMLHttpRequest();
}
//fro ie, etc
else
{
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

    //to get response from php
xmlhttp.onreadystatechange = function()
{
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
    //your code here
    }
}

    //open connection
    xmlhttp.open("GET","../project_php/login.php",true);
xmlhttp.send();

/*Note:
  GET -> method
  2nd paramater(../project_php/login.php) -> destination url
  3rd parameter -> true or false (use true!)

  on "your code here" comment, add your code if response from php successfully received.
*/

Upvotes: 1

mavili
mavili

Reputation: 3424

You can submit a form using a link like this:

<a href="javascript:{}" onclick="formanName.submit();"> linktext </a>

to ask for confirmation before sending, you can try:

<a href="javascript:{}" onclick="if(confirm('Message?')) { formName.submit();}"> linktext </a>

where formName is the name of the form you want to submit. I hope that helps.

Upvotes: 0

Related Questions