0

I have a problem calling a php file using javascript in codeignite.

This is my button:

<input type="button" name="update" value="Update" onClick="setUpdateAction();" />

And this is the script:

function setUpdateAction() {
  document.frmUser.action = "edit_user.php";
  document.frmUser.submit();
}

function setDeleteAction() {
  if(confirm("Are you sure want to delete these rows?")) {
    document.frmUser.action = "delete_user.php";
    document.frmUser.submit();
  }
}

The php file I want to call is inside the 'views' folder together with the php where the button code is located.

user0206
  • 1
  • 5
  • You are vulnerable to SQL injection, read this: http://stackoverflow.com/a/60496/3263412 – marionebl Feb 27 '14 at 07:32
  • 1
    I cannot see any question which makes sense to me. What do you want? What do you actually get? – ˈvɔlə Feb 27 '14 at 07:32
  • instead of just saying php page and php files please name the php files will make us understand what you want – krishna Feb 27 '14 at 07:35
  • I mean I have this code: function setUpdateAction() { document.frmUser.action = "edit_user.php"; document.frmUser.submit(); }. I just want to access the edit_user.php when the button is onclick(). I am using codeigniter. The php file I want to access in on the folder application/view/utility – user0206 Feb 27 '14 at 07:36

2 Answers2

0

Maybe you are searching for something like this:

HTML

<form action=edit_user.php method=post>
  [...]  
  <input type=button value=Update onclick=SetAction("update")>
  <input type=button value=Delete onclick=SetAction("delete")>
</form>

Javascript

function SetAction(action) {
  var form = this.parentNode;

  if (action === 'update') {
    form.action = "/edit_user.php";
  }
  else if (action === 'delete') {
    form.action = "/delete_user.php";
  }

  form.submit();
}
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
0

Now your script is in the views folder, which is wrong place for it (why it should be there?), imho. If I understood what you're trying to achieve, push it to the controllers folder and give that php-link like you would link any controller in CI. For example:

document.frmUser.action = "/delete_user";

and it should work. I mostly use jQuery, and in this way it operates properly. Imho, best way would be AJAX..maybe with jQuery. It gives you plenty of options, easier way to send data to the controller class etc.

Kurre
  • 1
  • 1