Submission form that will insert content on another page - php

Me and My friends are starting a website. I'm the only one who knows any type of coding. Since they don't know any programming language I'd like to make a form submission page that will allow them to just type in basic info, updates and have it generate and insert PHP or HTML into the main content page for them.
For example if name in box 1="Rob" I want it to insert <p>Rob #HH:MM MM:DD:YY</p>
And whatever info is typed into box 2 to be in the following paragraph.
I know something like above is possible with PHP and SQL, but I'm just kinda stumped as to know what it needs to be searched to learn it.
Thank you in advance for your help.

It sounds as though you don't necessarily even need any PHP for this. This is something you could just as easily do in the client itself. Of course, for something more elaborate, you're going to need to build this out more and can undoubtedly make use of a more elaborate system, but for your example, just piece together what you're aiming to join with some JavaScript.
I've assembled a sample at http://codepen.io/anon/pen/VjdPgm but here's the important bit:
HTML
<p>Box 1</p>
<input id="box1">
<p>Box 2</p>
<input id="box2">
<br/>
<button onclick="generate()">Generate</button>
<p>Result</p>
<textarea id="result" cols="100" rows="10"></textarea>
JavaScript
var generate = function() {
var output = "";
//Append the text for box1
var box1 = document.querySelector('#box1').value;
output = '<p>' + box1 + '#HH:MM MM:DD:YY</p>';
//Append the text for box2
var box2 = document.querySelector('#box2').value;
output += '<p>' + box2 + '#HH:MM MM:DD:YY</p>';
//Set the output in the result box
document.querySelector('#result').value = output;
}
You click on the button, it executes this JavaScript function that pulls the values from the boxes you filled in and drops the values into the inline templates.
Now, if you actually want this to save the logic somewhere, you're going to need to incorporate the logic to save this via PHP and potentially store somewhere with the rest of your templates or the like. For this, you'll need to consider a number of other aspects including validation, escaping and how you're designing your own pages to build them with their content (e.g. does this script overwrite an existing file or do you assemble the webpage based on some MySQL lookups), but this is a bit outside the scope of your question.
If you're utilizing a CMS of sorts, you might consider using something like the above example to generate the markup so they can just drop what they want in the CMS and avoid having to write all that other stuff yourself.

Related

Get data from HTML and save it as a PHP variable

I have started learning php and I have a question.Let's say I have the following html code:
<p id='tobeChanged'>I wil be changed throughout the execution<p>
This paragraph is not static.Its content can be changed from the user with a button which will produce a random number and will replace the paragraphs html.
E.g. from
p id='tobeChanged'>I wil be changed throughout the execution<p>
to
<p id='tobeChanged'>42<p><!--changed with a button-->
Now my question.Is it possible to pass the new produced value to a php variable?If possible i would like a long explanation.
Also i would like not to use forms(if possible).
Thanks In advance
You need to fire an AJAX request on that button click, that will send that value to server making php to read it.
You can do something like this (you need to include jQuery on page):
$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})
At PHP end, you will get the value in
$_POST['randNum']
Maybe that will help.

Find and store all innerhtml of div to php variable

I’m trying to store the content of a div to a variable.
Example:
<div class="anything">
<p>We don't know the content of this div</p>
</div>
I want to search for <div class="anything"> and store everything between opening and the end tag.
We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.
Is this possible with PHP, or is this only possible with JavaScript ?
PHP is not that intelligent. He doesn't even know what he says.
PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?
Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.
Like a red fish.
To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.
So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.
Quick example on how to parse a rendered page with simpleHtmlDom:
Let's say you know that your page will be available at http://mypage.com/mypage.php
$html = file_get_html('http://mypage.com/mypage.php');
foreach($html->find('div.anything') as $element)
echo $element->src . '<br>';
you probably need a combination of those.
In your Javascript:
var content = document.getElementsByClassName("anything")[0].innerHTML();
document.getElementByID('formfield').value(content);
document.getElementByID('hiddenForm').submit();
In your HTML/PHP File:
<form id="hiddenForm" action="path/to/your/script">
<input type="hidden" name="formfield" value="" />
</form>
In the script you defined in the form action:
if(!empty($_POST)){
$content = $_POST['formfield'];
// DO something with the content;
}
Alternatively you could send the data via AJAX but I guess you are new to this stuff so you should start slowly :)
Cheers!
steve
You could use JS to take the .innerHTML from the elements you wan and store them in .value of some input fields of a form and then use a submit button to run the PHP form handling as normal. Use .readOnly to make the input fields uneditle.

Passing img to textarea

I'm trying to make smileys work, so I'm going to post the full problem, maybe someone knows a better solution. I have this chat system, where you can click on a smiley and it's value gets passed to the <textarea> much like Google hangouts. Value is "smile_n" where 20 > n > 0. I store it like that in the database, and I have PHP code that's in charge of displaying the proper <img src="smile_n"> tag when parsing data from SQL, but when I pick a smiley, it will write "smile_n" in the <textarea>. Is there ways to change this?
Here's how I drop smileys into the <textarea> element:
$(".smilepick").click(function(){
$('#chatty').val($('#chatty').val()+(' ')+$(this).attr('href')+(' '));
var el = $("#chatty").get(0);
var elemLen = el.value.length;
el.selectionStart = elemLen;
el.selectionEnd = elemLen;
el.focus();
});
Can I somehow make it parse "smile_n" words into images, but keep the value that gets inserted into database "smile_n" so PHP code won't fail?
If you could use div with contenteditable, you could start with something like this:
HTML
<div id="editable" contenteditable="true">
Everything contained within this div is editable in browsers that support.
</div>
Jquery
var smile_ha_img = '<img src="http://placehold.it/16x16"/>';
$('#editable').keyup(function() {
$(this).html(function(i, v) {
return v.replace('smile-ha', smile_ha_img);
});
});
It will replace every smile-ha with the given image.
Try it here: http://jsfiddle.net/tb8vQ/1/ Type smile-ha into paragraph on 4th panel.
Then you can make the content of the div be copied to a hidden textarea to be send by your form as usual.
To Do
You must optmize it for the amount of smiles to check and replace.
After the function replace the string with the html, the carret position is placed on beginning of string (at least in my browser). There are a lot of answers here in Stackoverflow about how to solve this.
The keyup trigger used here would not be useful for your system if users doesn't type the smile code themselves. But you can change your function to be executed right after the user chose the emoticon.
Another approach
There are some WYSIWYG editors that allow you to choose which features you want to offer to your users. So maybe you could find one that you could hide all options but emoticons.

Is there a way to access previously created divs?

I'm creating divs with dynamic names using php such as:
echo "<div class=\"".$row['country']."\">"
So it's going to first create a series of divs such as ...
<div class="America">
//stuff goes in here
</div>
<div class="Germany">
//stuff goes in here
</div>
<div class="Singapore">
//stuff goes in here
</div>
But later on in the code after the "Germany" div has already been created, I'm going to make another mysql query to a different table and I want to access the "Germany" class and add content in it. It then becomes something like...
<div class="Germany">
<p> Germany has x number of people </p>
<p> The most popular car in Germany is x </p>
</div>
I know with Jquery there is the append() function. Is there something similar in PHP where I can access a div that's already been created and add stuff to it?
Note: all of this is in one php file that loads all the content when the page loads.
It can be done. You can use output buffering to capture all output. Then you can use an HTML DOM parser to modify that output. After that, you can flush the lot.
But this will work only once and it will seriously slow down your script. Don't do it, it smells of bad design.
Your best option would be to create multiple variables like
$css['America'];
$css['Germany'];
$general_output;
etc and, while you are building your site, just add info to the required variable.
Once you get to the end of the page, print them in the right order and you are done.
I think you can create an array like
$div["Amereica"] = "America"
And in future you can append value to this array. eg.
$div["Amereica"] .= 'text to append';
And finally you can use implode function or using other array functions you can crate final html.
If you really need to re-access that element, you're probably looking for DOM functions or phpQuery
Simple answer is no.
You can't change HTML code produced by PHP, because that code is already sent to browser, and PHP works on server side.
One of complicated answers is #GolezTrol answer.

Accessing text in a field placed by JS, via PHP

In PHP, in a particular CMS I am using a custom field, which works like google suggest.
As in, for each letter I type an SQL query is performed and matching records are displayed. When clicking on a record it fills the field with that record.
I am fairly certain this is all done with JavaScript.
I need to know how I can access the resultant content of that field, with the text placed through JS, before it is submitted so I can explode() it.
The CMS I am using is using mootools, so a solution relying on mootools would be ideal.
(This answer assumes that you have control over the markup of your forms (the form that requires a string "explosion" before submit) and/or you feel comfortable tinkering with whatever plugins you're using.)
first, make sure that you aren't submitting your form using an actual submit button (). We'll need to submit the form using javascript after fiddling with the field's contents.
next, make sure that your input box (the one you're grabbing text from) and your hidden inputs have unique ids. This will make it easier to query the DOM for the data we need.
Inside your form, in place of a "real" submit button, create a form button:
<form action="something.php" name="myform">
<input type="hidden" id="hiddenItem">
// SOME STUFF
<input type="text" id="autocomplete_field" value="whatever"/>
// SOME OTHER STUFF
<input type="button" value="Submit" onclick="processForm(this)"/>
</form>
Then, write a javascript function to process the string and submit the form:
processForm = function(el){
text = $('autocomplete_field').get('value');
// Lets assume the strings separates words (what you're exploding apart) using spaces
// something like 'DOGS CATS BIRDS PETS'
var array = text.split(' ');
// returns ['DOGS','CATS','BIRDS','PETS']
$('hiddenItem').set('value',array[0]);
// #hiddenItem now has the value 'dogs'
//SUBMIT THE FORM
el.getParent('form').submit();
};
Hope this helps!
You could try to use JS to send the field on some event (onkeyup?) to your php script. After it does it's part, store the result as a session variable and you can retrieve that later.
Try using jquery's get function.
Was that your question?

Categories