Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:20:35+00:00 2026-05-30T20:20:35+00:00

Using Codeigniter, I am trying to get a php script to run after a

  • 0

Using Codeigniter, I am trying to get a php script to run after a user clicks on a submit button. I have followed quite a few tutorials and have come up with the following code:

If it would help to see the actual page you can visit it here: http://rickymason.net/townbuilder
guest/guest to login (currently required)

In my view (structures.php), I have :

<div id="structures">
<h1>Build</h1>
<form name="buildForm" id="buildForm" method="POST">
    <select name="buildID" class="buildClass">
    <option value="0" selected="selected" data-skip="1">Build a Structure</option>
<?php foreach ($structures as $structure_info): ?>
<option name='<?php echo $structure_info['str_name'] ?>' value='<?php echo $structure_info['str_id'] ?>' data-icon='<?php echo $structure_info['str_imageloc'] ?>' data-html-text='<?php echo $structure_info['str_name'] ?><i>
    <?php echo $structure_info['timebuildmins'] ?> minutes<br><?php echo $structure_info['buy_gold'] ?> gold</i>'><?php echo $structure_info['str_name'] ?></option>
<?php endforeach ?>
    </select>
    <div id="buildSubmit">
        <input class="button" type="submit" value="Submit"/>
    </div>
</form>
</div>
<div id="output"> Result here</div>

This creates my select form and creates it in html. I then created a .js file to run the ajax:

$("#submit").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: '../php/build.php',        
data: "",
dataType: 'json',                     
success: function(data)          
{
  alert("success!");
} 
});
}); 

I tried to keep it as simple as I could, just for testing purposes. Javascript is new to me, and I find it extremely confusing for some reason. From what I can gather, this script should activate after the submit button is pressed. It then runs the build.php script, then if it returns an echo through json, it alerts success. right?

my build.php script is below:

<?php
    $query = "INSERT INTO user_structure (str_id, user_id) VALUES ('7', '7')";
    mysql_query($query) or die ('Error updating database');      
    echo "success";
?> 

Again, very simple…

Unfortunately, when I click submit, the page seems to “refresh” but nothing happens. I don’t expect anything to happen on the page, but the database doesn’t receive any values either.

I do not have the build.php or build.js files in a controller or model, they are outside the application directory and are accessed outside the CI uri/url structure.

As of right now, I am simply trying to successfully run a php script by clicking submit. My final goal is to take the str_id value that is created for the form, then pass that variable and the user_id into the build.php file, create the db entry, then refresh the page to display the updated DB info.

Obviously, it is not working! Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:20:35+00:00Added an answer on May 30, 2026 at 8:20 pm

    That script works with an element having id “submit” (because you use "#submit" as the selector)

    Use the click event instead of submit

    Either you can give an id to your submit like this

    and use like this

    $("#btnSubmit").click(function(e){
       // your rest of code
    });
    

    or use the class name

    $(".button").click(function(e){
       // your rest of code
    });
    

    But It is always better to use the id becaues there will be only one ( and should be) element with an id. More than one element can have the same class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using CodeIgniter (a PHP framework) to build an app, and I have an
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,
I'm trying to get simplepie to loop through a few rss feeds using codeigniter
I am trying to get started with the PHP MVC framework, CodeIgniter. I am
I am trying to extend the CodeIgniter controller. I have created MY_Controller.php file, the
I am trying to create a URL shortener using CodeIgniter 2. I have 2
I have a form and I am trying to submit data into database using
I am trying to create PDF file using DOM pdf and Codeigniter. I have
I trying to make my first AJAX with JSON call using jQuery and CodeIgniter.
I am using CodeIgniter to pass some parameters to my PHP page through $_POST

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.