sending data through submit using colorbox ajax - php

HTML
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" class="ajax cboxElement" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value="" />
</form>
</div>
Controller
function find()
{
$this->pageload_model->load_page();
$getquery = $this->input->get("q");
$data['find'] = $this->find_model->get_find_view($getquery);
$page['content'] = $this->load->view("template/findtemplate.php", $data);
echo json_encode($page);
}
This controller correctly generates my desired results when you manually go to the URI. It was slightly modified in the attempt to use jquery ui dialog. However, i'd much rather use colorbox or fancybox at this point.
find model:
public function get_find_view($q)
{
if (!$q) {
$html = "Search disrupted: <a href='". base_url()."'>Go back to main page </a>";
return $html;
} else {
$accountdata['found'] = $this->find_model->get_found_view($q);
$accountdata['create_category'] = $this->find_model->get_create_category_view($q);
return $this->load->view('find/find_view', $accountdata , TRUE);
}
}
the colobox ajax JS should be: $('ajax').colorbox()
I have the ajax class in 2 areas...a straight up link, which works with no problems, and the above mentioned submit button. What I am looking to do is send the input value to the find() controller, load the page, and open it via the colorbox window. I eventually would add a change to pushstate, so that the new URL / page can be crawled and shared.
I am having no luck. Thanks for the help! Will add any code you might need.

I haven't used colorbox before so I'm not 100% on this. I think what you need to do is serialize the data from your form and handle the ajax request yourself. So something like this:
HTML:
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" onsubmit="return category_form_submit();" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value=""/>
</form>
</div>
JQUERY:
function category_form_submit() {
$.get('/welcome/find?' + $('#category_form').serialize(), function(response){
$.fn.colorbox({html:response});
//here is where you could put your pushstate code
});
return false;
}

Related

Add items to array without refreshing the page?

http://alpha.ripfy.com/
As seen in the following demo, I have a YouTube video that I want to be able to play while adding items to the array in PHP. Sadly, this isn't possible from what I've tried because the page refreshes every time I add an item to the array.
Would there be any way of achieving this without the page refreshing (forcing the video to restart?)
Code:
<?php
if (isset($_POST['playlist'])) {
$playlist = $_POST['playlist'];
} else { // Else set my default list
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
}
if (isset($_POST['name1'])) {
$playlist[] = $_POST['name1'];
}
?>
<form method="post">
<?php
foreach($playlist as $song) {
?>
<input type="hidden" name="playlist[]" value="<?php echo $song?>">
<?php
}
?>
<input type="text" name="name1"/>
<input type="submit" name="submit1"/>
</form>
<iframe width="560" height="315" src="https://www.youtube.com/embed/pzB6CxChIQk" frameborder="0" allowfullscreen></iframe>
<?php
foreach ($playlist as $value) {
echo $value."<br>";
}
?>
Thanks for helping me out!
if you need that these information be storaged in database or anything on server sider don't use the convencional form post, use AJAX with JQuery.
First create a div(container)to render the list content where you need the information apears:
<div id="list_musics"></div>
Then create a page(i.e. page.ajax.php) that will treat your request. If you need you can put the information in a database or anything you want by this page. This page must return the content you want to render.
HTML:
<input type="button" id="ajaxcaller">
JQUERY:
$('#ajaxcaller').on('click', function(){
//AJAX CALL WITH POST METHOD
var text = $('input[name=name1]').val();
$.post(page.ajax.php,text,function(data){
//Render your content in the container created on HTML
$("#list_musics").html(data);
});
});
If you just need to show what you wrote on text input instead of storage or treat the information, you may just use JQUERY to render the information in the container you created.
$('#ajaxcaller').on('click', function(){
//AJAX CALL WITH POST METHOD
var text = $('input[name=name1]').val();
// PUT THE TEXT RIGHT AFTER THE CONTENT THAT ALREADY EXISTS IN THE CONTAINER
$("#list_musics").append(text);
});
Take a look here to see the sencond option:
https://jsfiddle.net/wqLf65ox/
Here's my answer. That fully works. In this script, the server checks the presence of a name1 request (post or get). If exists, it returns the string posted (only) and if doesn't, it returns what already was there. And the post() method posts (gets) the data and appends to the container using innerHTML+= data + "<br>";
evaluate the code, it should be self explanatory.
<?php
if (isset($_REQUEST['playlist'])) {
$playlist = $_REQUEST['playlist'];
} else { // Else set my default list
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
}
if (isset($_REQUEST['name1'])) {
// if exists, return plain text responce. NOT HTML
$playlist[] = $_REQUEST['name1'];
echo $_REQUEST['name1'];
}
else{
?>
<html>
<head>
<title>Demo</title>
<script src="jquery.js"></script>
</head>
<body>
<script>
text= ""
function onUpdate(){
text = document.getElementById("name1").value;
}
function handler(data){
document.getElementById('container').innerHTML += data+"<br>";
}
function post(){
onUpdate();
$.get("index.php", name1="+text, handler);
}
</script>
<input type="text" name="name1" id="name1"/>
<input type="submit" name="submit1" onclick="post()"/>
<iframe width="560" height="315" src="https://www.youtube.com/embed/pzB6CxChIQk" frameborder="0" allowfullscreen></iframe>
<br>
<div id="container">
<?php
foreach ($playlist as $value) {
echo $value."<br>";
}
?>
</div>
</body>
<?php };?>
Although, it works as intended, i don't see the point of using it. just plain js should work
You have to send your form using Ajax, eg. via jQuery.post() method.
After that you have to reload your container with list or add new item there with JavaScript.
Try using the $.post and $.get JQuery methods. One method might be to $.post back to a .php page that renders a container of html, then use $.get to retrieve just that html container and insert into the DOM.

Wordpress submit a form, update link with form input and go to that link & link is "#"?

This code is a form that results in a link being created with variable $ui.
<form method="post" name="form" onsubmit="#">
Name search:
<input id="ui" type="text" name="ui" />
<input type="submit" class="Submit" value="submit" />
</form>
Then the variable $ui is used to set the value of the data-filter attribute. When you click the link (after the form submits) it goes to "#" which performs a sorting/listing function and reloads the page without refreshing.
<?php if(isset($_POST["ui"]))
{
$ui = $_POST["ui"];
}
$Filterclass = strtoupper(str_replace(" ", "-", $ui));
?>
<a href="#" id="gallery_filter"
data-filter=".<?php echo $Filterclass; ?>">
<?php echo strtoupper($ui); ?></a>
Right now, the form submits and then the link is created, then you can click on it.
It works as-is, but I'd like to have the 'submit' button just open that link, the one with newly updated data-filter attribute.
I would ask this is Wordpress.exchange but I think is too advanced.
I won't write the whole process but modify your existing code as below(assuming jquery is already loaded)
<?php if(isset($_POST["ui"]))
{
$ui = $_POST["ui"];
echo '<script type="text/javascript">
jQuery(document).ready(function(e){
jQuery("#gallery_filter").click();
});
</script>';
}
$Filterclass = strtoupper(str_replace(" ", "-", $ui));
?>
<a href="#" id="gallery_filter"
data-filter=".<?php echo $Filterclass; ?>">
<?php echo strtoupper($ui); ?></a>

Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})

Passing a view as a dialog with codeigniter

What i would like to do, is instead of loading up a separate page to input data into a database, i would like to pop open a dialog, and upon submission, it would add data to the database and refresh the page.
For example: I'm on the index page and want to add another entry to a datagrid. I would click a button that adds a new entry. A dialog pops up, i add information into it, and hit submit. It would then refresh and remain on the index page.
This is what i currently have, but it doesn't do anything right now.
Index page:
<div id="createEntry">
<button>Create new item</button>
</div>
$("button")
.button().click(function(){
$.post('<?php echo base_url(); ?>test/create', function(response){
$('#dialog').html(response);
});
});
Create page
<div id="dialog" title="Create new test case">
<?php echo form_open(base_url() . 'test/create') ?>
<label for="title">Title</label><br />
<input type="text" name="title" /><br />
<label for="something">Something</label><br />
<textarea name="something"></textarea><br />
<input type="submit" name="submit" value="Create" />
</div>
<script>
$("#dialog").dialog();
</script>
Controller
public function create()
{
//...
//some code here for validation
//
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$var = $this->load->view('test/create', '', TRUE);
$this->load->view('templates/footer');
}
else
{
//adds to db
$this->test_model->set();
}
}
I feel like i'm close. There's just something i'm missing
Any help is appreciated.
Simple answer. I needed a <div id="dialog"> on the index page.

Safe way to update DIV with other page-content

I'm wondering what is the best and safest way to make a redirect in a div after submitting a form.
So, I have a form with a calulation inside a div. When the calculation is 0 I would like to show page_zero.php into this div instead of the form.
Here is some code:
form_page.php
<div class="form_wrapper">
<div id="PP_succes_engine" style=""></div>
<form id="SignupForm" action="" method="post" ONSubmit="xmlhttpPost('invoice_engine.php', 'SignupForm', 'PP_succes_engine', '<div class=\'PP_wait_loading\'></div>');return false; ">
<?php
//calculation
?>
<input name="tot_price" type="hidden" value="<?php echo $tot_price; ?>">
<input name="Submit" type="submit" value="Submit" class="type-button">
</div>
invoice_engine.php
<?php
session_start();
if ($_POST['tot_price'] == 0) {
// REDIRECT ME TO 'page_zero.php' BUT STAY INTO <div class="form_wrapper">
}
else {
// RUN OTHER SCRIPT
}
?>
if ($_POST['tot_price'] == 0){
include 'page_zero.php';
exit;
else ...
You can just include. Why redirect?
Life would be simpler and cleaner to implement using jQuery:
Have a read through: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Not exactly what you're looking for but all the elements are there.
Basically, you can call your invoice_engine.php and get it to return HTML that you can then 'inject' into a DIV on your page.

Categories