0

I am new to php and this forum. I have made a small code in php to add the countries in array and on click it must display the list of the countries.

i am unable to do it. I m sharing the code.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<h1> My Favorite country </h1>
<form action="" method="POST">

<input  type="text" name="country"></input><br>
<input   type="submit" name="Add" value="Add" ></input>

<input  type="submit" name="clear" value="clear"></input>
<input  type="submit" name="display" value="display"></input>

<?php
error_reporting(0);
$arr = array();
$country = $_POST['country'];


$add = $_POST['Add'];
$clear = $_POST['clear'];

$display = $_POST['display'];


if($add)
{

}
}
elseif($display)
{

print_r($arr);
}
else{
}
?>
</form>
<ol><? echo $value ?></ol>
</body>
</html>

Thanks!

TheStark
  • 45
  • 1
  • 1
  • 6
  • You need to share your PHP code. How else can we point out what you're doing wrong? – Barmar Jan 31 '18 at 23:55
  • Its also not jquery... – Lawrence Cherone Jan 31 '18 at 23:56
  • Please consider this: JQuery, or Javascript, is running on the client side in the browser. PHP is running on the server, and the result is sent to the client and displayed in the browser. Two different beasts. – KIKO Software Jan 31 '18 at 23:57
  • 1
    welcome to S.O , no one will spoon feed developer here,show some efforts 1st . – sumit Feb 01 '18 at 00:02
  • Not even remotely jQuery it is plain old javascript – Matthew Lagerwey Feb 01 '18 at 00:09
  • Improve on your Javascript skills first, then attempt PHP. There are a number of problems with your Javascript code (disregarding the fact you confuse it with jQuery which tells me it's not your code to begin with): 1) global variables; 2) array instantiated through `Array()` instead of the more succinct and friendly `[]`; 3) `array.length` calculated on every iteration of the `for` loop; 4) ugly line indentations. – Pyromonk Feb 01 '18 at 00:16
  • @Barmar code share above now – TheStark Feb 01 '18 at 01:06
  • @LawrenceCherone now made some php code pls check – TheStark Feb 01 '18 at 01:06
  • @KIKOSoftware thank you – TheStark Feb 01 '18 at 01:07
  • @RokoC.Buljan now chk pls sir. – TheStark Feb 01 '18 at 01:07
  • @sumit anyhelp now – TheStark Feb 01 '18 at 01:07
  • @MatthewLagerwey now php – TheStark Feb 01 '18 at 01:07
  • @Pyromonk i m new to languages – TheStark Feb 01 '18 at 01:08
  • 1
    You need to use a session variable or database to keep the array of countries between each form submission. – Barmar Feb 01 '18 at 01:11
  • @Barmar i want this for a one session. suppose i will add all countries and when click display it will show the countries. on page refresh or clear button everything from new – TheStark Feb 01 '18 at 01:14
  • 2
    We're not going to write the code for you, that's not how SO works. You need to do your own research about session variables, and try to write it yourself. If you can't get it working, post what you tried. When we asked you to show your code, we didn't mean that you should leave all the important parts blank. – Barmar Feb 01 '18 at 01:17
  • 2
    You should also realize that your php should be written separately so you don't confuse the fact that php is server side only. Your form should have an action where it calls the php script to process the information. The php then should render a page with the information. The process you are describing of rendering countries on a click can all be done with client side programming, no php required. – Matthew Lagerwey Feb 01 '18 at 01:23
  • 1
    You should get a good javascript book and get familiar with this [link](https://developer.mozilla.org/en-US/) website – Matthew Lagerwey Feb 01 '18 at 01:27
  • 1
    @dev, it doesn't matter if you're new or not. You deal with programming problems the same way you deal with other problems: you partition them and attempt them one at a time. You don't learn how to bake a cake bypassing learning how to knead (or find, or purchase) dough. I can see that you are new and that you are blindly copying code. You should invest into learning how to solve simpler problems first that require using a *single* language, ideally, not 2-3 at once. – Pyromonk Feb 01 '18 at 01:54

1 Answers1

0

The term "favorite country" implies one entity. So, if there are millions of users logged in at the same time and each user enjoys his/her own session, then at max in a session a user would only see one result, namely his/her favorite country. The sessions are not shared -- more info here.

The OP may wish to consider obtaining each user's favorite country and then store that information in a database. Then one may retrieve the stored data and assign it in the process to an array in order to iterate it and display the various countries (or additionally, one may wish to do some kind of statistical analysis with the data).

In the meantime, here's some basic PHP and HTML code :

<?php
error_reporting(E_ALL);
$fav_country = "";



if (isset($_POST["add"]) && $_POST["add"] === "add") {
     if (ctype_print($_POST['country'])) {

           $fav_country = $_POST['country'];

     };

     // code to store fav country in dbf:

}
else
if (isset($_POST["display"]) && $_POST["display"] === "display"){
     echo "nothing in database yet :)";
     // code to retrieve and display the fav countries stored in dbf:
}
?>
<html>
<head><title>testing</title></head>
<body>
<h1> My Favorite Country </h1>
<form action="" method="POST">
<input  type="text" name="country"><br>
<input  id="clear" type="reset" name="clear" value="clear">
<input type="submit" name="add" value="add">
<input  type="submit" name="display" value="display">
</form>
<div><p>My input: <span><?php echo $fav_country ?></span></p></div>
<script>
var d = document;
d.g = d.getElementById;
var clear = d.g("clear");
clear.onclick = function(){
    d.getElementsByTagName("span")[0].textContent="";
};
</script>
</body>
</html>

Some things to note: 1) Never trust input from user. Every POST or GET needs to be checked to make sure the data is what you're expecting, otherwise you could be in for some unpleasant surprises to say the least. I use ctype_print() to permit countries composed of multiple names, such as the United Kingdom.

2) If you're new to PHP, be sure to check out the online manual at http://www.php.net.

3) If you're going to work with HTML, use a validator. The input tag does not have a closing tag -- it stands alone.

Good luck to the OP -- we've all been newbies. But, study coupled with sufficient coding experience can transform a newbie into an expert -- it just takes some work! Also, the greater one's technical knowledge, the better one can formulate feasible technical requirements.

slevy1
  • 3,797
  • 2
  • 27
  • 33