I will be honest, even looking at the second posted project I see a jumbled mess.
It looks like you are just copying and pasting code with no real understanding of what you are putting together.
This is the main problem you are having. What you have is code that does not make sense and will never do what you want unless you gain a little bit of understanding.
I do have a working modal but to be honest I think you should first have a look at
Then start over again.
With just a little bit of using the resources ( online stuff ) in the link above you will see why what you have will never work and the code you need is not very complicated.