New element on a php array file - php

I'm a total noob on php, but a friend of mine asked for help and I thought I might do it..
I have this code/file here and want to find a way to add an element in this array via html file. I know it sounds noob, it does for me too, but please help, I've seen arrays, vectors and lists only on c++, tried to take a look at the documentation of php5 (since he want it in php 5) but I couldn't make it!
here it is...
<?php
$bledi = array('user12345', 'user2016', 'user5749852', 'user985658', 'HowToAddANewElement');
echo $result;
?>

You need a form with a text input in HTML and a send button.
When the form is submitted to the action page there you could put
your php.
You can make a global array ($bledi) and you will have an if condition there that says something like
if($_REQUEST['input_text'])
$bledi[] = $_REQUEST['input_text'];

Related

How to add an item to a PHP array on HTML user input

<?php
$friends = array('notch',);
foreach ($friends as $friend) {
echo '<img src="http://mcapi.ca/avatar/'.$friend.'"/>';
}
?>
I would like to change the item in the PHP array on HTML user input from a textarea. Could someone provide the HTML & PHP code in order for this to work? The array changing would mean that an image would update. Is this possible to do without having to reload the page for the image to show? I am new to PHP, so my knowledge is fuzzy. Help is greatly appreciated.
As all the comments already say:
No, this is not possible with PHP. PHP runs on the server, prepares a page (html output), and this page is sent to your browser. Therefore, user input can only happen after PHP has finished its work. What you want to do can only be done on the client side - have a look at JavaScript.
As said, you are talking about 2 different worlds(Client side/Front end and server side/Backend).
One thing you could do is, use something like KnockoutJS(or some MVVM/framework, there are several) and when the changes happens, you can interact with your php in execution time without having to reload the page as you were asking.
i have modify your code, hope so it will help you to sort out your problem.
<?php
$friends = array('notch'); // i have remove comma after 'notch'
<script>
var item = document.getElementById("put here your input text field id").value;</script>
foreach ($friends as $friend) {
echo '<img src="http://mcapi.ca/avatar/'.$friend.'"/>';
}
?>

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.

add text to random textarea in html (php)

I would like to know if something like this is possible for php:
Very-Pseudo-Code:
<body>
<textarea name="text1">
</body>
<?php
$A=mysqli_connect(...);
if($A){**text1.echo "Connection established";**}
$B=mysqli_select_database(...);
if($B){**text1.echo "Database selected";**}
?>
I want a life feedback that tells the user what is going on in the background. Kind of a life feed that consistently gives the user information. I played around with GET and POST methods and also with PHP_SELF, but nothing worked out.
In javascript it seems to be quite simple with something like
document.myform.append()
Is there something like this for php, or do i have to mix it with javascript?
(This is my first html/php project, i hope its not a silly question)

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.

Problems with <form> and Dynamically-generated links

Related directly to this post, I am having trouble implementing some sound advice given from #sean, because as you can see on this page:
http://www.onestopfasteners.com.au/checkout.php - I have wrapped the form tags around the table element, but the form just doesn't seem to be working, and nothing ever gets "POST"ed either. I've changed the code around abit to experiment, but haven't found a solution, and no search anywhere has proven to be useful yet.
I'd appreciate any help at all. I'm completely baffled!
Thanks!
P.S. I thought that maybe the fact that I am wrapping form elements around dynamically generated content could be why the form isn't working, but that doesn't make much sense to me and, I've done it before, so that can't be it, can it?
Code:
I know, it's long, apologies in advance. :)
<?php
// (c) code removed ;) problem solved - thanks to everyone who helped!
?>
I think your problem is with:
function submit() {
document.myform.submit();
}
Try:
function submit() {
document.getElementById('ct_form').submit();
}
It looks like you are using jQuery in the page so you could also use:
function submit() {
$('#ct_form').submit();
}
Your using javascript to submit the form, but you are referencing document.myform which doesn't exsist.
try this instead.
document.getElementById('ct_form').submit()
// do sumbit first form of document
document.forms[0].submit()
document.getElementById is not necessary here. document.myform relies on NAME attribute of FORM element, by the way

Categories