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 7600117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:50:06+00:00 2026-05-30T22:50:06+00:00

My original goal was to get a php file to execute on a button

  • 0

My original goal was to get a php file to execute on a button press. I used ajax. When the javascript was in the view, it worked.

However, I tried to switch the javascript to its own .js file and include it in the header. It doesn’t work anymore. I am confused.

the model code:

public function insert_build($user_id)
{
    $query = "INSERT INTO user_structure (str_id, user_id) VALUES ('7', '$user_id')";
    mysql_query($query) or die ('Error updating database');
}

Something interesting to note here is that when I include $user_id as a value, it completely negates my headertemplate. As in, it simply doesnt load. When I replace $user_id with a static value (i.e. ‘7’) it works no problem.

This is my view code :

<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 id ="btnSubmit" class="button" type="submit" value="Submit"/>
        </div>
    </form>
</div>

Heres my .js file :

$(".button").click(function(e){
    e.preventDefault();
    $.ajax({
    type: "POST",
    url: "<?php $this->structure_model->insert_build($user_id) ?>",         //the script to call to get data          
    data: "",                        //you can insert url arguments here to pass to api.php
                                        //for example "id=5&parent=6"
    dataType: 'json',                //data format      
    success: function(data)          //on receive of reply
    {
    alert("success!");
    } 
    });
}); 

I am almost sure I know the problem: That structure_model->insert_build($user_id) ?> doesn’t work when its outside the view. Though, I dont know the alternative.

I excluded the header file. I confirmed that the .js file is indeed being directed to the correct path.

Could someone please explain the correct way to do this? Thank you!

  • 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-30T22:50:08+00:00Added an answer on May 30, 2026 at 10:50 pm

    Did you move your javascript to a .js that is being directly accessed by the browser? I.E: If you view source, so you see the <?php ... ?> in the javascript code?

    To me, it sounds as though the PHP is not getting parsed. If this is not the case, then can you please clarify.

    If you need to include PHP variables in your javascript, you should use CI to generate the JS page for inclusion. You can even create a View that is purely JS and call it like a normal page.

    Otherwise, if you want to seperate the JS from CI, you should reference JS variables instead of PHP. Then in your CI page somewhere, define them with a <script>var jsVar = <?php echo phpvar(); ?></script> tag.

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

Sidebar

Related Questions

I tried to perform XSLT transform of a XSD file. My goal is in
Original Question I am writting a logging class where the goal is to be
Let's say file test.php looks like this: <?php echo 'Hello world.'; ?> I want
I've been having trouble trying to get PIL to nicely downsample images. The goal,
My original goal here was a modal dialog, but you know, Android didn't support
Original Question I want to be able to generate a new (fully valid) MP3
Original Question I am looking for a function that attempts to quantify how distant
Original Question What I'd like is not a standard C pre-processor, but a variation
original string is a,d,k I want to remove all , and make it to
ORIGINAL Q: I'm wondering if anyone has had experience of migrating a large Cobol/PL1

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.