insert url parameter in mysql with codeigniter - php

I need your help!
I am new to codeigniter and I am trying to pass a fetch parameter from url, get me the value using the following
My example url is the following: http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816
Where what I am rescuing is the value "NroContacto", I have managed to bring me the value with the following line in the controller:
'NroContacto' => $this->input->get('NroContacto');
and show it in the view with the following:
<td>
Numero Folio: <?php echo $nrocontacto; ?>
</td>
that way I have managed to show it in the user's view, but now I can't find a way to save that value in the database when the user clicks the "save" button

You have not shared much information related to your issue. I am assuming that you need two server calls to save the URL parameter into data.
First "http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816", is used to render the page with a form to submit.
Second, When the user clicks the submit button in the form, data is stored in a database.
If this is the case, on first page, you can collect nrocontacto as you have already done in your code and add a hidden input field <input type="hidden" name="nrocontacto" value="<?php echo $nrocontacto; ?>"> inside the form in the view file.
When the user submit the form, you can grab the value using $nroContacto = $this->input->post('NroContacto', true); (this code goes to form processing controller method). Now you can use CI query builder $this->db->insert('tableName', array('field_name' => $nroContacto)); (Note: add other required fields for insert array) to save it to the Database.

Codeigniter support seo friendly urls so you can arrange your url like this
http://example.com/index.php/news/local/metro/crime_is_up
for your url it will be
http://localhost/infocargacasosfinal/index.php/creacionacta/1/103386816
Now you can fetch like this
'NroContacto' => $this->uri->segment(3, 0);
now pass the value to your view page.
You can get detailed documentation about url segments here
https://codeigniter.com/userguide3/libraries/uri.html

Related

Laravel 5, link to previous form page and retrieve post input

I have two pages. The search and the found.
On the search page there are searching fields.
The 'found' page gives back the results of search.
In the template of found page there is a button.
<a class="btn btn-default" href="/search">Back</a>
When I click on the "BACK" button, the input data, which had been typed in, disappears. How could I make a link, which retains the input fields?
You can store the searched variable in the "found" page, retrieve it when you come back to "search," and tell Laravel to "forget" it after you leave that page.
Store in /found:
Session::put("search", Input::get("search_field_name"));
Retrieve in /search when coming back:
Session::get("search");
Forget in /search after you've set the search field variable:
Session::forget("search");
This will display your last search even if you are not coming back from somewhere else. What you can do is only get and forget whenever your URL::previous() is a /found URL.
In case you want to store all the inputs at once, so you don't have to handle each of them sepparately, you can do with Session::put("search", Input::all());
Later on, you can retrieve them both as an array or as an object in order to access its properties:
$input_array = Input::all();
$input_object = (object)$input_array;
Then, access a property called, for instance, "search_term,", you can use the array:
$input_array["search_term"];
Or the object:
$input_object->search_term;
Hope this helps!

Retrieve POST-data from view in controller

I'm trying to do something very basic: retrieve a post value from a hidden field. The hidden field is obviously in my view file and I want to retrieve the value in my controller. I'm using the framework SimpleMVCFramework.
I have a hidden field in my projects.php file (the list with projects). When you click on a project, a method in the controller renders the clicked project and the corresponding page. This corresponding page is called project.php
The hidden field in my projects.php view:
<form method="post" action="project.php">
<input type="hidden" name="project-id" value="<?php echo $project['id'];?>">
</form>
This hidden form is displayed correctly in my lists with projects. I checked them in the console.
In my ProjectController.php, I try to retrieve the data using
$data['id'] = $_POST['project-id'];
Then, I send the $data variable with the rendered page, so that I can use the id. So every project in projects.php has a hidden file that outputs correctly. When I try and click on a project, it brings me to project.php, but when I check out the $data variable, the id is just empty.
The routing works like a charm, because e.g. $data['title'] = "Project"; works great and is visible when I check the $data variable. When I change
$data['id'] = $_POST['project-id'];
to
$data['id'] = "foobar";
the id in project.php isn't empty anymore, but shows foobar. So I guess that something goes wrong with retrieving the value.
I also tried to remove the action=".." from the form, but that also didn't work.
The thing I'm trying to achieve is so simple, that I don't understand what is going wrong. Is it possible that the problem lies with the framework and that the code is right?
Thanks in advance and sorry for my bad English.

Lost data when moving page to page

I have a form to post with for example "title" data, once the submit button is pressed, the model will be used to process the data. the controller then uses the processed data to display in a view to the user. That is the flow I follow.
After getting the view, the user will choose to edit something. he thus needs to get back to the edittable form (same as the original one) to fix the title data. Here is the form
<?php form_open("blog/edit_updated")?>
Title:<input type="text" col="30" value="<?=$edit[0]['title']?>" name="edit_title"/><br/>
Comment:<br/><textarea name="edit_comment" width="20" col="5"><?=$edit[0]['comment']?></textarea><br/>
<input type="hidden" name="postcomment" value="TRUE"/>
<input type="submit" value="Update"/>
<?=form_close()?>
That form will call the following controler function
public function edit_updated()
{
if(!file_exists('application/views/blog/edit_succeed.php'))
{
show_404();
}
else
{
print_r($this->url_title);
if($this->blog->update_row_with_title(XXXXX,$_POST['edit_title'],$_POST['edit_comment']))
$this->load->view('blog/edit_succeed');
}
}
the function update_row_with_title is only used to seach and update the specified item with exact match of the given title. I test and it always return one (UPDATE command is used). XXXX is the title of the NOT_YET_TO_FIX title used to search in the database, the rest of parameters are those newly entered fields that will be used for SET in UPDATE command. However, XXXXX is always empty. Could you tell me a way to retrieve the original title ?
Summary
Form 1 (with data)--> post --> use data as part of url for GET --> fix the form (with new data) --> UPDATE [right here I lost the original data that is used as an identity for db searching]
It's a bit ambiguous your description, I'm not sure I understand what you want, but I'll try to offer a solution.
If you need the old title in the controller method you can actually send that as a parameter to it in the URL. To do this you have to make the following changes:
in the view containing the form change the argument of form_open to something like
<?php form_open("blog/edit_updated/" . $edit[0]['title'])?>
add an argument to the edit_updated() controller method like this
public function edit_updated($old_title = null) {
...
...
}
and now in the controller method you can replace that XXXXXX variable with $old_title.
Was this helpful ?

filling the form in declared link

I have to declare an html link in my php file, i can do that in the following way
echo "<a href='razz.com'>Update</a>";
the link razz.com has a form fields(text type) in it, what i want to do is that when i open the link the form fields present in the link needs to be filled by the values that i declare in my initial php page.
the values of the form fields that needs to be filled are obtained from database.
How can i do that? Any tutorials or code snippet are appreciated.
Thank you.
Code snippet of what i am looking for:
a user fills form information which needs to go into database for storage:
the form is as follows:
<p>Title:<input type="text" name="title"/></p>
<p>Report No:<input type="text" name="rno"/></p>
<p>URL:<input type="text" name="url"/></p>
now after filling the form, the data will be stored in the database . A page with echo's successful with form information and a link to update the previously entered information will be displayed.
now again this update link contains the form structure , so instead of entering the information which was previously entered. the information already entered needs to be fetched and displayed in the form area.
You would need to use GET request parameters and the $_GET array on the page that is accepting the request to pass values to your link, but only if the http://razz.com/ index page actually accepts your URL parameters.
For instance (see the stuff and otherstuff GET keys in the URL):
echo "<a href='http://razz.com/?stuff=yes&otherstuff=yadayada'>Update</a>";
Then the razz.com index(.php):
$stuff = $_GET['stuff']; // with the link above, equal to yes
$otherstuff = $_GET['otherstuff']; // with the link above, equal to yadayada
echo "
<h2>$stuff</h2>
<p>I got $otherstuff.</p>
";
This would echo on razz.com/index(.php):
<h2>stuff</h2>
<p>I got yadayada.</p>
You could also use the $_POST array, but this would require more work and you would need to have a reason to do this (such as the data you're passing is transitory). You could do this by triggering a form, which you could also do a GET request with.
However, your question is not really that well worded. If you can provide more context and direction, that would help.

replace input field with data from mysql php

I created this layout of successive text input fields,
1- Enter data into empty fields
2- Click on button which submits to a php page that updates into database
Now the problem is that i want when i return to the main page again the empty field is replaced with data just added but there are still other empty fields to enter new data.
How can i establish that?
Thanks in advance.
You haven't given a lot of detail but here goes!
You could build your inputs like this:
<input type="text" name="age" value="<?php echo $age; ?>">
When the form first loads, it won't have values for variables like $age, so the input will appear empty. Have the form submit via POST to the same PHP file, run your validation checks, and if everything passes, insert into to your database. (Is it required that you write to the database at this point, or should it wait until the second section is filled out?)
You'll need to use some kind of conditional statement to display the second part of the form. Depending on how complex this is, or whether users will be returning later, you could:
Read the data back out of the
database to check for completeness,
and then display the second part.
Set a variable to track what stage of the form you're in, and based on that, display different sections to be completed.
If you have a way of tracking what stage of the process you're in, you could do something like this:
$formStage = 2;
function isReadOnly($formStage='')
{
if ($formStage == 2) {echo 'READONLY';}
}
and then in your HTML:
<INPUT NAME="realname" VALUE="Hi There" <?php isReadOnly($formStage)?>>

Categories